苏飞论坛

标题: httphelper POST XML的时候会出问题,不知道什么原因 [打印本页]

作者: inghot    时间: 2012-9-20 16:28
标题: httphelper POST XML的时候会出问题,不知道什么原因
代码如下:
[C#] 纯文本查看 复制代码
httphelper objhttp = new httphelper();
            objhttp.isToLower = false;
            HttpItem objHttpItem = new HttpItem();
            objHttpItem.URL="https://URL";
            objHttpItem.Method = "post";
            objHttpItem.ContentType = "text/xml";
            objHttpItem.Postdata=str;
            string rtnStr = objhttp.GetHtml(objHttpItem);


结果在类的53行出现错误:
未将对象引用设置到对象的实例。

但是我直接用下面的方法就不会:
[C#] 纯文本查看 复制代码
public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
        {
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "post";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "text/xml";
            //request.ContentType = "application/x-www-form-urlencoded";
            byte[] buffer = encoding.GetBytes(strPostdata);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
            {
                return reader.ReadToEnd();
            }
        }

不知是啥原因。


作者: inghot    时间: 2012-9-20 16:37
加个
[code=csharp]objHttpItem.Encoding = "UTF-8";[/code]
就可以了。
呵呵。

作者: 站长苏飞    时间: 2012-9-20 16:51
可能是编码的问题,我记下这个Bug了,有时间优化一下,你这个其它是分析编码的问题,
加上编码就没有问题了。

作者: liu67667    时间: 2014-10-27 07:18
受教了学习中……
作者: coody    时间: 2016-3-31 17:10
没有解决这个问题
作者: coody    时间: 2016-4-1 08:06
解决也是另外一个帖子解决的!现在不知道新版本有无解决这个问题
解决是用网上的解决的
百度 post c# xml
问题出现时xml有中文的时候会乱码!提交不成功!




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4