我在模拟一个http请求,代码如下:
string url3 = "http://restar.gmcc.net:9087/ResConfigure/restar/resDataQuery.action";
HttpWebRequest request4 = (HttpWebRequest)HttpWebRequest.Create(url3);
request4.Method = "POST";
request4.Headers.Add("Accept-Language: zh-cn");
request4.Headers.Add("Accept-Encoding: gzip, deflate");
request4.AllowAutoRedirect = true;
request4.KeepAlive = true;
request4.Referer = "http://restar.gmcc.net:9088/webmaster/air/managerview/index.jsp";
request4.Host = "restar.gmcc.net:9087";
request4.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
request4.ContentType = "application/x-www-form-urlencoded";
request4.CookieContainer = cc;
string data2 = "object=v4-system-messageRead_getMessage&READ_USERID=guozhibiao&userName=guozhibiao";
byte[] postdata = Encoding.ASCII.GetBytes(data2);
request4.ContentLength = postdata.Length;
Stream outstream4 = request4.GetRequestStream();
outstream.Write(postdata, 0, postdata.Length);
outstream.Close();
"Stream outstream4 = request4.GetRequestStream();"这一句会抛出"未将对象引用设置到对象"的异常,非常困惑,希望哪位大侠帮一下,急用,谢谢!
要学会看异常找BUG,空指针异常表明了你调用实例方法的实例为NULL,就是request4为空。
HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(url3);
我添加监视看了request4,很多值都有,刚才奇迹般运行出来一次,但又不行了,头疼啊
这个错误就是:那个关键的地方需要赋值的,结果是null值,所以会报这个错,好好找找吧