Http之Post传文件之处理 Java vs .Net vs Python

文件数据如下:

 

代码
 1 <?xml version="1.0" encoding='GB2312'?>
 2 <ufinterface billtype="defdoc" filename="合同档案数据.xml" isexchange="Y" proc="add" replace="Y" roottag="bill" sender="FL">
 3     <bill id="123455">
 4         <billhead>
 5             <!--自定义项档案内容编码,不能为空-->
 6             <doccode>11</doccode>
 7 
 8             <!--自定义项档案内容名称,不能为空-->
 9             <docname>月明合同</docname>
10 
11             <!--自定义项档案内容系统属性,可不用填写-->
12             <docsystype></docsystype>
13 
14             <!--所属公司,不能为空,公司名称-->
15             <pk_corp>集团</pk_corp>
16 
17             <!--自定义项档案内容PK-->
18             <pk_defdoc></pk_defdoc>
19 
20           <!--上级档案名称:超级填写上级档案名称-->
21             <pk_defdoc1></pk_defdoc1> 
22 
23             <!--此外默认:合同档案-->
24             <pk_defdoclist>合同档案</pk_defdoclist>
25 
26             <!--封存标志,默认为N-->
27             <sealflag></sealflag>
28         </billhead>
29     </bill>
30     <bill id="123456">
31         <billhead>
32             <!--自定义项档案内容编码,不能为空-->
33             <doccode>22</doccode>
34 
35             <!--自定义项档案内容名称,不能为空-->
36             <docname>月明开发合同</docname>
37 
38             <!--自定义项档案内容系统属性,可不用填写-->
39             <docsystype></docsystype>
40 
41             <!--所属公司,不能为空,公司名称-->
42             <pk_corp>集团</pk_corp>
43 
44             <!--自定义项档案内容PK-->
45             <pk_defdoc></pk_defdoc>
46 
47           <!--上级档案名称:超级填写上级档案名称-->
48             <pk_defdoc1>月明合同</pk_defdoc1> 
49 
50             <!--此外默认:合同档案-->
51             <pk_defdoclist>合同档案</pk_defdoclist>
52 
53             <!--封存标志,默认为N-->
54             <sealflag></sealflag>
55         </billhead>
56     </bill>
57 </ufinterface>
58 
 
JAVA:
 
代码
  1 package nc.pfxx.test;
  2 import java.io.BufferedInputStream;
  3 import java.io.BufferedOutputStream;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.net.HttpURLConnection;
  9 import java.net.URL;
 10 import java.util.zip.GZIPInputStream;
 11 import java.util.zip.GZIPOutputStream;
 12 
 13 import org.apache.axis.utils.XMLUtils;
 14 import org.w3c.dom.Document;
 15 
 16 public class SendXmlFile {
 17 
 18 /*    public static void main(String[] args) {
 19         SendXmlFile f = new SendXmlFile();
 20         int result = f
 21                 .sendFile(
 22                         new File("c:\\test.xml"),
 23                         "http://10.88.12.201:80/service/XChangeServlet?account=0002&receiver=1411000001");
 24         System.out.println(result + "=" + f.getMsg());
 25     }*/
 26 
 27     private static String msg;
 28     private static Document msgDoc;
 29 
 30     public static String getMsg() {
 31         return msg;
 32     }
 33 
 34     
 35     public static HttpURLConnection getConnection(String url, boolean bcompress)
 36             throws Exception {
 37         try {
 38             if (bcompress)
 39                 url = (new StringBuilder()).append(url)
 40                         .append("&compress=true").toString();
 41             URL realURL = new URL(url);
 42             HttpURLConnection connection = (HttpURLConnection) realURL
 43                     .openConnection();
 44             connection.setDoOutput(true);
 45             connection.setRequestProperty("Content-type""text/xml");
 46             connection.setRequestMethod("POST");
 47             System.out.println((new StringBuilder()).append("获得连接: ").append(
 48                     url).toString());
 49             return connection;
 50         } catch (IOException e) {
 51             throw new Exception("在获取连接中出错:", e);
 52         }
 53     }
 54 
 55     private static void sendfileimpl(File file, String url) {
 56         // Document msgDoc;
 57         HttpURLConnection curconnection;
 58         msgDoc = null;
 59         curconnection = null;
 60         try {
 61             curconnection = getConnection(url, false);
 62             sendMessage(file, false, curconnection);
 63             msgDoc = receiveResponse1(curconnection, false);
 64         } catch (Exception e) {
 65             e.printStackTrace();
 66         } finally {
 67             if (curconnection != null)
 68                 curconnection.disconnect();
 69         }
 70         System.out.println(XMLUtils.DocumentToString(msgDoc));
 71         
 72     }
 73 
 74 
 75     public static Document receiveResponse1(HttpURLConnection connection,
 76             boolean bcompress) throws IOException {
 77         try {
 78             Document doc = null;
 79             InputStream is = null;
 80             if (bcompress)
 81                 is = new GZIPInputStream(connection.getInputStream());
 82             else
 83                 is = connection.getInputStream();
 84             doc = XMLUtils.getDocumentBuilder().parse(is);
 85             return doc;
 86         } catch (Exception e) {
 87             throw new IOException("读回执时出错:" + e);
 88         }
 89     }
 90 
 91     private static void sendMessage(File file, boolean bcompress,
 92             HttpURLConnection curconnection) throws IOException {
 93         try {
 94             if (bcompress) {
 95                 System.out.println("压缩传输...");
 96                 GZIPOutputStream zipout = new GZIPOutputStream(curconnection
 97                         .getOutputStream());
 98                 InputStream input = new FileInputStream(file);
 99                 try {
100                     byte buffer[] = new byte[1000];
101                     int length;
102                     while ((length = input.read(buffer, 01000)) != -1)
103                         zipout.write(buffer, 0, length);
104                     input.close();
105                     zipout.close();
106                 } catch (IOException e) {
107                     System.out.println("Couldn't compress file!");
108                 }
109             } else {
110                 System.out.println("非压缩传输...");
111                 BufferedOutputStream out = new BufferedOutputStream(
112                         curconnection.getOutputStream());
113                 BufferedInputStream input = new BufferedInputStream(
114                         new FileInputStream(file));
115                 byte buffer[] = new byte[1000];
116                 int length;
117                 while ((length = input.read(buffer, 01000)) != -1)
118                     out.write(buffer, 0, length);
119                 input.close();
120                 out.close();
121             }
122         } catch (IOException e) {
123             throw new IOException("写消息时出错:" + e);
124         }
125     }
126 
127 
128     public static Document getMsgDoc() {
129         return msgDoc;
130     }
131 
132     public static void setMsgDoc(Document msgDoc) {
133         SendXmlFile.msgDoc = msgDoc;
134     }
135     
136     
137     public static void main(String[] args) {
138         SendXmlFile sxf = new SendXmlFile();
139         sxf.sendfileimpl(new File("D:/Backup/桌面/back/房间信息模板.xml"), "http://localhost:80/service/XChangeServlet?account=01&receiver=0001");
140     }
141 }
142 

 

C#:
代码
 1   string url = "http://192.168.0.198/service/XChangeServlet?account=01&receiver=0001";
                string content =。。。
 2                 byte[] requestBuffer = System.Text.Encoding.Default.GetBytes(content);
 3                 WebRequest request = WebRequest.Create(url);
 4                 request.Method = "POST";
 5                 request.ContentType = "application/x-www-form-urlencoded";
 6                 request.ContentType = "text/xml";
 7                 request.ContentLength = requestBuffer.Length;
 8                 using (Stream requestStream = request.GetRequestStream())
 9                 {
10                     requestStream.Write(requestBuffer, 0, requestBuffer.Length);
11                     requestStream.Close();
12                 }
13 
14                 WebResponse response = request.GetResponse();
15                 using (StreamReader reader = new StreamReader(response.GetResponseStream()))//, Encoding.Default))
16                 {
17                     strMsg = reader.ReadToEnd();
18                     reader.Close();
19                 }  

 

 
Python:
代码
 1 #-*- coding:utf-8 -*-
 2 ps_url = "http://192.168.0.198/service/XChangeServlet?account=01&receiver=0001"
 3 
 4 #filename = "D:/PythonWork/ufinterfaceBasDoc.xml"
 5 filename="D:/PythonWork/Testdefdoc.xml"
 6 file = open(filename, 'r')
 7 content = file.read()
 8 #content = bytearray(content)
 9 
10 import urllib2
11 req = urllib2.Request(url = ps_url,  headers = {'Content-type':'text/xml'})
12 req.add_data(content)
13 rep = urllib2.urlopen(req)
14 print rep.read()

 

 

 

posted @ 2010-11-16 15:49  我想去长安  阅读(682)  评论(1编辑  收藏  举报