如下代码将XML串发送给一个URL,在url端抓包时,能收到码流,但是XML串内容为空.
请高手指点,多谢!!!!HttpURLConnection httpurlconnection = null;
URL url = null;
url = new URL("http://10.188.16.11:8008/AToB");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.connect();
String resInfo = "$xml=" + imResponse.EntityString;
httpurlconnection.getOutputStream().write(resInfo.getBytes("UTF-8"));
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();

解决方案 »

  1.   


                URL url = new URL(strURL);
        uc = url.openConnection();
        uc.setDoOutput(true);
        uc.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
        uc.setRequestProperty("Accept-Language", "zh-cn");
        uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
        uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
        uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        uc.setRequestProperty("Host", strHost);
        uc.setRequestProperty("Connection", "Keep-Alive");
        uc.setRequestProperty("Cache-Control", "no-cache");            String strHeader = ""; 
                strHeader += "-----------------------------7d318fd100112\r\n";
                strHeader += "Content-Disposition: form-data; name=\"fUpload\"; filename=\"E:\\xmltemp.xml\"\r\n";
                strHeader += "Content-Type: text/xml\n";
                String strTail = ""; 
                strTail += "-----------------------------7d318fd100112\r\n";
                strTail += "Content-Disposition: form-data; name=\"cbUpload\"\r\n\n";
                strTail += "upload\r\n";
                strTail += "-----------------------------7d318fd100112--\r\n\n\n";
                
        DataOutputStream dos = new DataOutputStream(uc.getOutputStream());     dos.write(strHeader.getBytes());
                dos.writeBytes("\n");            // 直接把string写入dos
                //byte[] b = strXML.getBytes("UTF-8");
                //dos.write(b, 0, b.length); // 通过xml文件把数据写入dos
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(postxmlfile);
                OutputFormat format = new OutputFormat(doc);
                format.setIndenting(true);
                format.setLineWidth(0);
                format.setPreserveSpace(true);
                XMLSerializer serializer = new XMLSerializer(dos, format);
                serializer.asDOMSerializer();
                serializer.serialize(doc);            dos.writeBytes("\n");
                dos.write(strTail.getBytes());
                dos.close();
      

  2.   

    imResponse.EntityString 为xml字符串内容