[100分求助]HttpWebRequest.GetResponse()问题!

hsbhljl 2009-12-14 12:51:23
为了获取话费余额信息,可是在WebResponse Response = Request.GetResponse();这句这里出错。
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Net.WebException: 远程服务器返回错误: (500) 内部服务器错误。
在 System.Net.HttpWebRequest.GetResponse()
……

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

    public string GetHuaFei(string num)
{
string rl;
//http://www.sd.chinamobile.com/newecare/bankpay.do?searchName=&search=&menuid=netcharge&payphone=15066991234&balance=+&accept_type=+&sum_fee=+&payphoneVal=15066991234&repayphoneVal=15066991234&bankname=ABC&menuid=netcharge这是用ieHTTPHeaders截取出来的

//string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?searchName=&search=&menuid=netcharge&payphone=" + num + "&balance=+&accept_type=+&sum_fee=+&payphoneVal=" + num + "&repayphoneVal=" + num + "&bankname=ABC&menuid=netcharge";
string url="http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=" + num;
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);//?act=writePayInfo&payphone=" + num

WebResponse Response = Request.GetResponse();

Stream resStream = Response.GetResponseStream();

StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
string str = sb.ToString();
//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(" ", start);
int end = str.IndexOf("元", start + 1) + 1;
return str.Substring(start, end - start);
}
...全文
983 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 mngzilin 的回复:]
分,我要分100
[/Quote]
其实加UserAgent这个解决方法,我在网上也看到过,但是当时不够细心,没注意自己是用的WebRequest而不是HttpWebRequest,所以当时没成功,所以发疯了似的来CSDN问各位大大了,呵呵。
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 sdfkfkd 的回复:]
楼主的URL用IE不能访问啊
应该有一个登录的过程吧,要不不可能直接这样查询的
那不是谁的余额你都能看到啦
[/Quote]
不需要登录过程。但是好像只能查询山东省的,呵呵~~我就是发现了这个漏洞,所以想做一个查询话费余额的东西玩玩~~
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 mngzilin 的回复:]
分,我要分100
[/Quote]
多谢啦~!
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
分,我要分100
mngzilin 2009-12-14
  • 打赏
  • 举报
回复

\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充值号码为:  \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  \t\t\t\t\t\t\t\t\t15066991234\t\t\t\t\t\t\t\t\t \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当前话费余额:  \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  \t\t\t\t\t\t\t\t\t6.28\t\t\t\t\t\t\t\t\t元 \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 \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  \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;\"> 元(输入的金额必须为整数)\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支付方式:  \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
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 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();
[/Quote]
楼主,快给分吧。
特别 2009-12-14
  • 打赏
  • 举报
回复
楼主的URL用IE不能访问啊
应该有一个登录的过程吧,要不不可能直接这样查询的
那不是谁的余额你都能看到啦
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
楼主记得加分,记得先消消火。互相交流才是王道。
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
已经解决了,哈哈,在下面加上红色这句:

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();
fengjian_428 2009-12-14
  • 打赏
  • 举报
回复
虽然以下代码报错 但同一个网址用IE也访问不了。 能否给个能访问的

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();
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jin20000 的回复:]
UP,
PS:来回答问题的人,都是真心的想帮忙或者探讨的.........问题解决的途径有什么候就是需要不断地提问回答,才知道问题出在那,楼主太着急了!
[/Quote]
我本来以为描述的够清楚了,就是因为出现“远程服务器返回错误”,结果还是误导了大家以为是代码存在根本性问题。后来我说了,在浏览器中访问正常,而在程序里是“远程服务器返回错误”。
作为提问者,提问的不够详细是我的错误。对于回答者,我也没什么资格批评什么,也就不说啦。。
到现在为止,仍然没有有效的答案呀。。
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
这是我返回的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 \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 \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>"
jin20000 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 mngzilin 的回复:]
引用 6 楼 hsbhljl 的回复:
把不正确的答案贴上来,不是浪费时间是什么


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

你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。
[/Quote]
[Quote=引用 14 楼 mngzilin 的回复:]
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone='" + num+"'";
[/Quote]
UP,
PS:来回答问题的人,都是真心的想帮忙或者探讨的.........问题解决的途径有什么候就是需要不断地提问回答,才知道问题出在那,楼主太着急了!
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 silentwins 的回复:]
前来围观楼主的高度……
[/Quote]
那怎样才算是谦虚?我只是想说某些回答问题的,居然还在乎别人谦虚不谦虚。
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 mngzilin 的回复:]
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone='" + num+"'";
[/Quote]
我试过了,不行的。直接在IE访问也会说参数错误!
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
问题找到了,少加了单引号:
string url = "http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone='" + num+"'";
silentwins 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hsbhljl 的回复:]
可能某些人觉得俺不够谦虚,可能是他们水平没达到那个高度吧。
[/Quote]

前来围观楼主的高度……
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
10楼这位估计不是用的这个url测试的。
http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234
hsbhljl 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 mngzilin 的回复:]
你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。
[/Quote]
你测试的url,是我给的那个吗?
是这一个吗?
http://www.sd.chinamobile.com/newecare/bankpay.do?act=writePayInfo&payphone=15066991234
mngzilin 2009-12-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hsbhljl 的回复:]
把不正确的答案贴上来,不是浪费时间是什么
[/Quote]

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

你的代码和1楼的代码,我在我机子上调试正常,也返回了正常的html。
加载更多回复(9)

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧