首页 新闻 搜索 专区 学院

C#未将对象引用设置到对象的实例怎么解决

0
悬赏园豆:10 [已关闭问题] 关闭于 2013-10-19 10:42

我在模拟一个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();"这一句会抛出"未将对象引用设置到对象"的异常,非常困惑,希望哪位大侠帮一下,急用,谢谢!

Allenthandy的主页 Allenthandy | 初学一级 | 园豆:136
提问于:2013-10-18 15:33
< >
分享
所有回答(2)
1

要学会看异常找BUG,空指针异常表明了你调用实例方法的实例为NULL,就是request4为空。

 HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(url3);

我只看看不说话 | 园豆:291 (菜鸟二级) | 2013-10-18 17:34

我添加监视看了request4,很多值都有,刚才奇迹般运行出来一次,但又不行了,头疼啊

支持(0) 反对(0) Allenthandy | 园豆:136 (初学一级) | 2013-10-18 17:43
0

这个错误就是:那个关键的地方需要赋值的,结果是null值,所以会报这个错,好好找找吧

W宁宁 | 园豆:522 (小虾三级) | 2013-10-19 08:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册