代码如下:
[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();
}
}
不知是啥原因。
|