HttpWebRequest.GetResponse()问题!

http://topic.csdn.net/u/20091214/12/14799189-15fd-456a-a193-58a18074a6f9.html

 

注意HttpWebRequest  和 WebRequest的区别!

 

为了获取话费余额信息,可是在WebResponse Response = Request.GetResponse();这句这里出错。

HTML code
  
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Net.WebException: 远程服务器返回错误: (500) 内部服务器错误。 在 System.Net.HttpWebRequest.GetResponse() ……


我在网上搜了好多资料,都不行。如果谁能帮忙解决,100分归他了!

C# code
public string GetHuaFei(string num) { WebClient wb = new WebClient(); string str = wb.DownloadString("http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=" + num); //int start = str.IndexOf("<div class=\"tqxq_nr\"");  int start = str.IndexOf("当前话费余额"); if (start == -1) { return null; } start = str.IndexOf("<td", start); if (start == -1) { return null; } start = str.IndexOf("&nbsp;", start); int end = str.IndexOf("元", start + 1) + 1; return str.Substring(start, end - start); }

 

 

依然是同样的错误!
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Net.WebException: 远程服务器返回错误: (500) 内部服务器错误。
  在 System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
  在 System.Net.WebClient.DownloadString(Uri address)
  在 System.Net.WebClient.DownloadString(String address)
  在 huafei.GetHuaFei(String num) 位置 d:\My Documents\Visual Studio 2008\WebSites\HuaFei\App_Code\huafei.cs:行号 27
  --- 内部异常堆栈跟踪的结尾 ---
<!-- End_rbody_61894269//-->

#4楼 得分:0 回复于:2009-12-14 13:11:51
回答之前请自己先测试一下,测试起来不麻烦,也少浪费别人时间:)
<!-- End_rbody_61894297//-->

#5楼 得分:0 回复于:2009-12-14 13:26:59
引用 4 楼 hsbhljl 的回复:
回答之前请自己先测试一下,测试起来不麻烦,也少浪费别人时间:)


楼主看来挺nb的,以前也见过你发的帖子,没有一点谦虚的样子。你这样提问题也是浪费别人时间。
<!-- End_rbody_61895102//-->

#6楼 得分:0 回复于:2009-12-14 13:39:11
引用 5 楼 mngzilin 的回复:
楼主看来挺nb的,以前也见过你发的帖子,没有一点谦虚的样子。你这样提问题也是浪费别人时间。

我是个新手。问题的关键是,我把错误提示贴出来,完全被人给无视了。如果换个网址,上面的代码,包括我贴出来的,都没问题,现在我想知道的就是,这个网址该怎么解决。
如果知道就回答,不知道就别乱回答,想回答也行,自己先试试好不好?把不正确的答案贴上来,不是浪费时间是什么!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<!-- End_rbody_61895505//-->

#7楼 得分:0 回复于:2009-12-14 13:40:39
WebResponse Response = Request.GetResponse();
如果如楼主所说是这句出错,很有可能是:

1.你部署的提供service的接口可否正确解析参数,是否接口本身有异常
2.若能正确解析参数,并能获取到你要的数据,那请查查你的网络连接问题

希望能帮到你。
<!-- End_rbody_61895548//-->

#8楼 得分:0 回复于:2009-12-14 13:48:19
引用 7 楼 wuzhicheng5 的回复:
WebResponse Response = Request.GetResponse();
如果如楼主所说是这句出错,很有可能是:

1.你部署的提供service的接口可否正确解析参数,是否接口本身有异常
2.若能正确解析参数,并能获取到你要的数据,那请查查你的网络连接问题

希望能帮到你。

我自己糊涂了,没有把问题完全说清楚。
http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234
这个网址在浏览器中打开,完全没问题,放在程序里,不管是WebRequest、HttpWebRequest,还是WebClient,都是说
……服务器无法处理请求。 ---> System.Net.WebException: 远程服务器返回错误: (500) 内部服务器错误。
这个网址是中国山东移动的,可能对方做了某些限制。我接触.net也就半年时间,基本挺薄弱的,很多问题都不知道,所以诚心来提问题。可能某些人觉得俺不够谦虚,可能是他们水平没达到那个高度吧。
<!-- End_rbody_61895805//-->

#9楼 得分:0 回复于:2009-12-14 13:50:00
引用 5 楼 mngzilin 的回复:
楼主看来挺nb的,以前也见过你发的帖子,没有一点谦虚的样子。你这样提问题也是浪费别人时间。

你教训的是,我自己没把问题说清楚,而是想到什么就写了什么,自己有点糊涂,不好意思了,呵呵
<!-- End_rbody_61895868//-->
#10楼 得分:0 回复于:2009-12-14 13:51:06
引用 6 楼 hsbhljl 的回复:
把不正确的答案贴上来,不是浪费时间是什么


在这里回答问题的,并不是每个人都会回答正确的。更何况你的代码在你的机子上调试的。由于环境配置不一样,在别人机子上调试结果可能完全不一样。

你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。
<!-- End_rbody_61895914//-->

#11楼 得分:0 回复于:2009-12-14 13:54:55
引用 10 楼 mngzilin 的回复:
你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。

你测试的url,是我给的那个吗?
是这一个吗?
http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234
<!-- End_rbody_61896062//-->

#12楼 得分:0 回复于:2009-12-14 14:23:49
10楼这位估计不是用的这个url测试的。
http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234
<!-- End_rbody_61897152//-->

#13楼 得分:0 回复于:2009-12-14 14:40:38
引用 8 楼 hsbhljl 的回复:
可能某些人觉得俺不够谦虚,可能是他们水平没达到那个高度吧。


前来围观楼主的高度……
<!-- End_rbody_61897828//-->

#14楼 得分:0 回复于:2009-12-14 14:49:00
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone= ' " + num +"'" ;
<!-- End_rbody_61898146//-->

#15楼 得分:0 回复于:2009-12-14 14:51:29
引用 14 楼 mngzilin 的回复:
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone='" + num+"'";

我试过了,不行的。直接在IE访问也会说参数错误!
<!-- End_rbody_61898239//-->

#16楼 得分:0 回复于:2009-12-14 14:54:12
引用 13 楼 silentwins 的回复:
前来围观楼主的高度……

那怎样才算是谦虚?我只是想说某些回答问题的,居然还在乎别人谦虚不谦虚。
<!-- End_rbody_61898329//-->


#17楼 得分:0 回复于:2009-12-14 14:55:07
引用 10 楼 mngzilin 的回复:
引用 6 楼 hsbhljl 的回复:
把不正确的答案贴上来,不是浪费时间是什么


在这里回答问题的,并不是每个人都会回答正确的。更何况你的代码在你的机子上调试的。由于环境配置不一样,在别人机子上调试结果可能完全不一样。

你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。

引用 14 楼 mngzilin 的回复:
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone='" + num+"'";

UP,
PS:来回答问题的人,都是真心的想帮忙或者探讨的.........问题解决的途径有什么候就是需要不断地提问回答,才知道问题出在那,楼主太着急了!
<!-- End_rbody_61898369//-->

#18楼 得分:0 回复于:2009-12-14 14:57:41
这是我返回的html,用你的url:

" <html>\t <head>\t\t <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\t\t <link href=\"/newecare/new/css/sd_business.css\" rel=\"stylesheet\"\t\t\ttype=\"text/css\" />\t\t <title>山东移动网上营业厅_系统提示 </title>\t\t <SCRIPT LANGUAGE=\"JavaScript\"> <!--function doLoad(){ try{ parent.changeMainHeight(520); }catch(e){}}\tfunction onKeyDown()\t{\tif ( (event.altKey) || ((event.keyCode == 8) &&\t(event.srcElement.type != \"text\" &&\tevent.srcElement.type != \"textarea\" &&\tevent.srcElement.type != \"password\")) ||\t((event.ctrlKey) && ((event.keyCode == 78) || (event.keyCode == 82)) ) ||\t(event.keyCode == 116) ) {\t  event.keyCode = 0;\t  event.returnValue = false;\t  }\t}\tdocument.onkeydown = onKeyDown;\t\tfunction stop(){  \treturn false;\t}\tdocument.oncontextmenu=stop;//--> </SCRIPT>\t </head>\t <body style=\"padding:10px;\">\t\t <table width=\"370\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\t\t\t <tr>\t\t\t\t <td class=\"st_l_t\"> </td>\t\t\t\t <td class=\"st_t_c_bg\"> </td>\t\t\t\t <td class=\"st_r_t\"> </td>\t\t\t </tr>\t\t\t <tr>\t\t\t\t <td class=\"st_l_c_bg\">\t\t\t\t\t&nbsp;\t\t\t\t </td>\t\t\t\t <td class=\"st_bg\">\t\t\t\t\t <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\t\t\t\t\t\t <tr>\t\t\t\t\t\t\t <td>\t\t\t\t\t\t\t\t&nbsp;\t\t\t\t\t\t\t </td>\t\t\t\t\t\t\t <td width=\"15\">\t\t\t\t\t\t\t\t <a href=\"javascript:;\"> </a>\t\t\t\t\t\t\t </td>\t\t\t\t\t\t </tr>\t\t\t\t\t </table>\t\t\t\t\t <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"\t\t\t\t\t\tstyle=\"margin-bottom:20px;\">\t\t\t\t\t\t <tr>\t\t\t\t\t\t\t <td width=\"80\" align=\"center\">\t\t\t\t\t\t\t\t <img src=\"/newecare/new/images/icon_big_error.gif\" width=\"64\" height=\"64\" />\t\t\t\t\t\t\t </td>\t\t\t\t\t\t\t <td class=\"text_st_c\" style=\"padding-left:15px;\">\t\t\t\t\t\t\t\t输入的参数无效 ,请重新输入\t\t\t\t\t\t\t </td>\t\t\t\t\t\t </tr>\t\t\t\t\t </table>\t\t\t\t </td>\t\t\t\t <td class=\"st_r_c_bg\"> </td>\t\t\t </tr>\t\t\t <tr>\t\t\t\t <td class=\"st_l_b\"> </td>\t\t\t\t <td class=\"st_c_b_bg\"> </td>\t\t\t\t <td class=\"st_r_b\"> </td>\t\t\t </tr>\t\t </table>\t </body> </html>"
<!-- End_rbody_61898444//-->

#19楼 得分:0 回复于:2009-12-14 15:03:04
引用 17 楼 jin20000 的回复:
UP,
PS:来回答问题的人,都是真心的想帮忙或者探讨的.........问题解决的途径有什么候就是需要不断地提问回答,才知道问题出在那,楼主太着急了!

我本来以为描述的够清楚了,就是因为出现“远程服务器返回错误”,结果还是误导了大家以为是代码存在根本性问题。后来我说了,在浏览器中访问正常,而在程序里是“远程服务器返回错误”。
作为提问者,提问的不够详细是我的错误。对于回答者,我也没什么资格批评什么,也就不说啦。。
到现在为止,仍然没有有效的答案呀。。
<!-- End_rbody_61898635//-->


#
虽然以下代码报错 但同一个网址用IE也访问不了。 能否给个能访问的
C# code
           
           
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> string url = " http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234 " ; HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url); // ?act=writePayInfo&payphone=" + num WebResponse Response = Request.GetResponse();
<!-- End_rbody_61899108//-->


#21楼 得分:0 回复于:2009-12-14 15:25:47
已经解决 了,哈哈,在下面加上红色这句:

HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);//?act=writePayInfo&payphone=" + num
        Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        WebResponse Response = Request.GetResponse();
<!-- End_rbody_61899470//-->

#22楼 得分:0 回复于:2009-12-14 15:27:21
楼主记得加分,记得先消消火。互相交流才是王道。
<!-- End_rbody_61899554//-->


#23楼 得分:0 回复于:2009-12-14 15:28:19
楼主的URL用IE不能访问啊
应该有一个登录的过程吧,要不不可能直接这样查询的
那不是谁的余额你都能看到啦
<!-- End_rbody_61899607//-->

#24楼 得分:0 回复于:2009-12-14 15:30:33
引用 21 楼 mngzilin 的回复:
我已经解决了,哈哈,在下面加上红色这句:

HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);//?act=writePayInfo&payphone=" + num
       Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        WebResponse Response = Request.GetResponse();

楼主,快给分吧。
<!-- End_rbody_61899696//-->

#25楼 得分:0 回复于:2009-12-14 15:35:33
HTML code
           
           
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> \t\t\t\t\t < table id =\"table1\" width =\"100%\" border =\"0\" cellspacing =\"0\"\t\t\t\t\t\t\tcellpadding=\"0\" class =\"subbox\" > \t\t\t\t\t\t\t < tr > \t\t\t\t\t\t\t\t < td height =\"30\" width =\"30%\" align =\"right\" class =\"title_bgcolor text_subbox_title\" > \t\t\t\t\t\t\t\t\t充值号码为: &nbsp;&nbsp; \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t\t < td align =\"left\" class =\"subboxrb\" > \t\t\t\t\t\t\t\t\t &nbsp;&nbsp; \t\t\t\t\t\t\t\t\t15066991234\t\t\t\t\t\t\t\t\t &nbsp; \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t </ tr > \t\t\t\t\t\t\t\t\t\t\t\t\t\t < tr > \t\t\t\t\t\t\t\t < td height =\"30\" align =\"right\" class =\"title_bgcolor text_subbox_title\" > \t\t\t\t\t\t\t\t\t当前话费余额: &nbsp;&nbsp; \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t\t < td bgcolor =\"#FFFFFF\" align =\"left\" class =\"subboxrb\" > \t\t\t\t\t\t\t\t\t &nbsp;&nbsp; \t\t\t\t\t\t\t\t\t6.28\t\t\t\t\t\t\t\t\t元 &nbsp; \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t </ tr > \t\t\t\t\t\t\t\t\t\t\t\t\t\t < tr > \t\t\t\t\t\t\t\t < td bgcolor =\"#FFFFFF\" height =\"30\" align =\"right\" class =\"title_bgcolor text_subbox_title\" > \t\t\t\t\t\t\t\t\t请输入充值金额:\t\t\t\t\t\t\t\t\t &nbsp; \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t < td bgcolor =\"#FFFFFF\" width =\"50%\" align =\"left\" class =\"subboxrb\" > \t\t\t\t\t\t\t\t\t &nbsp;&nbsp; \t\t\t\t\t\t\t\t\t < INPUT type =\"text\" name =\"moneyTemp\" id =\"moneyTemp\" \t\t\t\t\t\t\t\t\t\tstyle =\"width:80px;height:20px;border:1px solid #87b5d7;\" > &nbsp; 元(输入的金额必须为整数)\t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t </ tr > \t\t\t\t\t\t\t < tr > \t\t\t\t\t\t\t\t < td class =\"title_bgcolor text_subbox_title\" > \t\t\t\t\t\t\t\t\t < div align =\"right\" > \t\t\t\t\t\t\t\t\t\t支付方式: &nbsp;&nbsp; \t\t\t\t\t\t\t\t\t </ div > \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t\t < td height =\"30\" bgcolor =\"#FFFFFF\" width =\"70%\" align =\"left\" class =\"subboxrb\" > \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t网上交费\t\t\t\t\t\t\t\t\t < img src =\"\" width =\"116\" height =\"22\" border =\"0\"\t\t\t\t\t\t\t\t\t\talign=\"middle\" > \t\t\t\t\t\t\t\t </ td > \t\t\t\t\t\t\t </ tr > \t\t\t
<!-- End_rbody_61899874//-->

#27楼 得分:0 回复于:2009-12-14 16:34:00
引用 26 楼 mngzilin 的回复:
分,我要分100

多谢啦~!
<!-- End_rbody_61902146//-->

#28楼 得分:0 回复于:2009-12-14 16:35:36
引用 23 楼 sdfkfkd 的回复:
楼主的URL用IE不能访问啊
应该有一个登录的过程吧,要不不可能直接这样查询的
那不是谁的余额你都能看到啦

不需要登录过程。但是好像只能查询山东省的,呵呵~~我就是发现了这个漏洞,所以想做一个查询话费余额的东西玩玩~~
<!-- End_rbody_61902212//-->


#29楼 得分:0 回复于:2009-12-14 16:37:08
引用 26 楼 mngzilin 的回复:
分,我要分100

其实加UserAgent这个解决方法,我在网上也看到过,但是当时不够细心,没注意自己是用的WebRequest而不是HttpWebRequest,所以当时没成功,所以发疯了似的来CSDN问各位大大了,呵呵。




你可能感兴趣的:(.net,浏览器,css,IE,中国移动)