IP138.com 获取IP地址出错?

lf44785170 2010-08-26 08:48:16
                Uri uri = new Uri(@"http://www.ip138.com/ip2city.asp");
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();

以前一直是可以得到外网IP的,最近就不行,提示

"服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF"

但是当先在浏览器访问 http://www.ip138.com/ip2city.asp 后,程序就能正常运行,并且访问其他显IP网址也可以,不知道要改哪了
...全文
955 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lf44785170 2010-08-27
  • 打赏
  • 举报
回复
搞不定,请大家帮忙提供几个好用的提取IP的网址吧.
wy811007 2010-08-26
  • 打赏
  • 举报
回复
学习下。。
civilman 2010-08-26
  • 打赏
  • 举报
回复
服务器提交了协议冲突.Section=ResponseHeader Detail=CR 后面必须是LF

这个错误貌似是微软没有容忍不符合RFC 822中的httpHeader必须以CRLF结束的规定的服务器响应所导致的,以至于无法使用软件登陆校内,让我很郁闷,后来尝试了各种方法,2种有效

方法一:

采用WEB编ASP.NET的话可用:

web.config文件里加入
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
允许系统容忍(tolerant)只以CR或LF结尾的hearder信息



方法二:

用于winform中,在Debug文件夹里新建一个文件名为:APPNAME.exe.config 的文件,APPNAME为应用程序的名字,然后用记事本打开,添加以下内容即可:

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
lf44785170 2010-08-26
  • 打赏
  • 举报
回复
楼上的,IP138网站又不是我的,我怎么改得了,我只是普通电脑想得到IP罢了,估计是他们见太多人这样访问,占用资源,故意做了设置,如果游览器访问就可以,代码访问就错误.
wuyq11 2010-08-26
  • 打赏
  • 举报
回复
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据

int start= all.IndexOf("[") + 1;
int end=all.IndexOf("]",start);
string tempip = all.Substring(start, end - start);
Console.WriteLine(tempip);

sr.Close();
s.Close();
Console.ReadLine();
孟子E章 2010-08-26
  • 打赏
  • 举报
回复
一个解决方案是在application.config或web.config文件里加入

<system.net>

<settings>

<httpWebRequest useUnsafeHeaderParsing="true" />

</settings>

</system.net>
只在此山中 2010-08-26
  • 打赏
  • 举报
回复
我这也可以
宝_爸 2010-08-26
  • 打赏
  • 举报
回复
我这里可以啊

Uri uri = new Uri(@"http://www.ip138.com/ip2city.asp");
System.Net.WebRequest wre = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wre.GetResponse().GetResponseStream();

StreamReader srd = new StreamReader(s);
string res2 = srd.ReadToEnd();


res2中得到的是


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> ����IP��ַ </title>
</head>
<body style="margin:0px"><center>����IP��ַ�ǣ�[203.110.131.5] </center></body></html>


乱码是因为我这里是英文系统
lf44785170 2010-08-26
  • 打赏
  • 举报
回复
谢谢 civilman 的提示,但我在 程序.exe.config 配置文件添加
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
运行就会提示配置文件错误,无法启动,重新编译也不行,这文件没研究过,不知道哪不对了,要不要添加到项目中什么的,我的是WinForm NET2.0 VS2008 XP IE6.0

111,125

社区成员

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

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

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