首页 新闻 会员 周边

http post请求,java和net的问题

0
悬赏园豆:5 [已解决问题] 解决于 2015-12-28 14:54

我用fiddler和C#写的一个请求XML

var client = new HttpClient();
var content = new StringContent(strXML, Encoding.GetEncoding("GBK"));
//content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
var result = client.PostAsync(apiUrl, content).Result;
return result.Content.ReadAsStringAsync().Result;

都报异常,返回的异常信息

<?xml version="1.0" encoding="gb2312"?><PACKET><HEAD><SystemCode></SystemCode><ResponseType>0</ResponseType><ErrorMessage><![CDATA[The element type "CarOwner" must be terminated by the matching end-tag "</CarOwner>".]]></ErrorMessage></HEAD></PACKET>

 接口供应商给我说的意思是我这里对流长度做限制了

改用java写是正常的

public static String net(String strUrl,  String method)
            throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            URL url = new URL(strUrl);  
            URLConnection con = url.openConnection();  
            con.setDoOutput(true);  
            con.setRequestProperty("Pragma:", "no-cache");  
            con.setRequestProperty("Cache-Control", "no-cache");  
            con.setRequestProperty("Content-Type", "text/xml");  
  
            OutputStreamWriter out = new OutputStreamWriter(con  
                    .getOutputStream());      
            String xmlInfo=readFileContent(FILEXML);
            System.out.println("urlStr=" + strUrl);  
            System.out.println("xmlInfo=" +xmlInfo);  
            out.write(xmlInfo);
            out.flush();  
            out.close();  
            BufferedReader br = new BufferedReader(new InputStreamReader(con  
                    .getInputStream()));  
            String line = "";  
            for (line = br.readLine(); line != null; line = br.readLine()) {  
                System.out.println(line);  
            }  
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

大神谁知道是怎么回事,困扰一星期了

胡晓凯的主页 胡晓凯 | 初学一级 | 园豆:2
提问于:2015-12-24 16:48
< >
分享
最佳答案
0

问题分析:

         用C#出错,从错误消息中:The element type "CarOwner" must be terminated by the matching end-tag "</CarOwner>". 说明在读取xml时出错,认为你的xml没有结束标记</CarOwner>

          用Java正常,说明xml文件是没有问题的。

          只能是在读取XML时采取的字符集不对,导致读取XML是没有读完。并且还可以说明你的XML中肯定是有中文,并且读取结束的地方,就在读取中文后没多远。

修改方案:

       Encoding.GetEncoding("GBK")   改用UTF-8

收获园豆:5
乐享程序员 | 小虾三级 |园豆:930 | 2015-12-25 13:28

谢谢了。我之前调试的时候用的Encoding.UTF8,所以就没有再这个方面想,换成Encoding.GetEncoding("UTF-8")后正常了,我又查了下UTF8和UTF-8的区别 UTF-8 区别

胡晓凯 | 园豆:2 (初学一级) | 2015-12-28 14:54
其他回答(2)
0

有中文么,查看编码格式是否正确,你用了GBK,注应该在头里面写清楚,在你的text/xml换成text/xml;charset=utf-8试一试。

至于说到长度的问题,应该不会出现问题的,因为httpclient会处理好的,它会把你写入数据的长度算出来,然后放到http头块里面,这个http协议的实现,你不需要管。你试一下是不是中英文的问题,或者假如你有特殊字符也会出现被中断的问题。

ensleep | 园豆:1682 (小虾三级) | 2015-12-25 09:26
0

1、断点检查你的XML是不是真的闭合了;

2、你的ContentType注释掉干嘛?或者你可以直接看StringContent构造函数的第三个是什么,直接设置成text/xml不得了。

然后再试试

烽火情怀 | 园豆:380 (菜鸟二级) | 2015-12-25 17:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册