关与WEBREQUEST和WEBRESPONSE两个类获取网页数据的问题!

digitalghost 2003-10-16 11:43:34
我想请问,为什么用如下代码获得的网页数据总是和目标网页有一些出入,好像总是比目标网页要长,出现部分重复数据,还有一个问题就是用这段代码获得的网页数据是对于某些网站总是会返回500服务器内部错误???请各位帮忙解决!!!!



System.Net .WebRequest webrq=null;
System.Net.WebResponse webrs=null;
string result=null;
try
{
webrq=System.Net.WebRequest.Create (UrlRoot+this.NodeUrl );
webrs=webrq.GetResponse ();

System.IO.Stream stream=webrs.GetResponseStream();
byte[] buffer=new byte [webrs.ContentLength ];

while(
stream.Read (buffer,0,buffer.Length )!=0)
result+=System.Text .Encoding .GetEncoding ("GB2312").GetString (buffer);

stream.Close ();
webrs.Close ();
}
catch(Exception ex2)
{
return;
}
finally
{
if(webrs!=null)
webrs.Close ();
}//webrq.Timeout =10000;

...全文
62 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
digitalghost 2003-10-18
  • 打赏
  • 举报
回复
是的,我已经试验过了,用URLENCODE方法处理了以后的连接不能用,相反用下面方法处理的就可以用。。。。

byte[] temp=System.Text .Encoding .Default .GetBytes(str);
temp=System.Web .HttpUtility .UrlEncodeToBytes (temp,0,temp.Length );
temps=System.Text .Encoding .Default .GetString (temp).Replace (".","%2e");
digitalghost 2003-10-18
  • 打赏
  • 举报
回复
是的,我已经试验过了,用URLENCODE方法处理了以后的连接不能用,相反用下面方法处理的就可以用。。。。

byte[] temp=System.Text .Encoding .Default .GetBytes(str);
temp=System.Web .HttpUtility .UrlEncodeToBytes (temp,0,temp.Length );
temps=System.Text .Encoding .Default .GetString (temp).Replace (".","%2e");
digitalghost 2003-10-18
  • 打赏
  • 举报
回复
大家说一下,究竟有什么好办法???
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
还不是很明白,请楼上的大哥说清楚一些,谢谢!
cnhgj 2003-10-17
  • 打赏
  • 举报
回复
Server.UrlEncode("欧美电影");
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
那个服务器内部错误的问题我发现是怎么回事了,
就是我提交WEB请求的时候提交的字符串是
"http://10.1.138.110/index.asp?Path=OD\1.欧美电影"
好像是提交
"http://10.1.138.110/index.asp?Path=OD%5C1%2E%C5%B7%C3%C0%B5%E7%D3%B0"
就不会出错了,但是究竟怎么样将上面的那个字符串转换成下面那个呢??
用.NET的System.Text命名空间下的哪个类,怎么样处理呢?? 谢谢!
saucer 2003-10-17
  • 打赏
  • 举报
回复
the output from my code:

http://10.1.138.110/index.asp?Path=OD\1.%c5%b7%c3%c0%b5%e7%d3%b0&TestData=%ba%ec%ba%d3%d0%c2%ce%c5

http://10.1.138.110/index.asp?Path=OD%5c1.%c5%b7%c3%c0%b5%e7%d3%b0&TestData=%ba%ec%ba%d3%d0%c2%ce%c5

did you try these URLs?
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
顶一下啦。。。。。
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
还是不行,怎么办啊?????。。。。。。。
saucer 2003-10-17
  • 打赏
  • 举报
回复
try


using System;
using System.Threading;
using System.Globalization;
using System.Web;
using System.Text.RegularExpressions;

class TestUrl
{
public static string MatchEval(Match m)
{
return HttpUtility.UrlEncode(m.Value,System.Text.Encoding.GetEncoding("GB2312"));
}

public static void Main()
{
string s = @"http://10.1.138.110/index.asp?Path=OD\1.欧美电影&TestData=红河新闻";
Console.WriteLine(Regex.Replace(s,@"[\u4e00-\u9fa5]+",new MatchEvaluator(MatchEval)));

int nPos = s.IndexOf("?");
string sPath = s.Substring(0,nPos);

string sQuery = s.Substring(nPos+1);

string[] pairs = sQuery.Split('&');
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("GB2312");
foreach (string sValue in pairs)
{
sb.Append("&");

string[] sNV = sValue.Split('=');
if (sNV.Length == 2)
{
sb.Append(HttpUtility.UrlEncode(sNV[0],gb) + "=" + HttpUtility.UrlEncode(sNV[1],gb));
}
else
sb.Append(sValue);
}

if (sb.Length > 0)
{
sQuery = sb.ToString(1,sb.Length - 1);
}

string sURL = sPath + "?" + sQuery;

Console.WriteLine(sPath + "?" + sQuery);
}
}
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
我用System.Web.HttpUtility .UrlEncode处理了以后还是得不到那个结果,URL里面的“."没有被处理,而我需要的那个结果却是处理了"."的!究竟怎么解决啊。。。急
digitalghost 2003-10-17
  • 打赏
  • 举报
回复
各位帮忙研究一下吧,谢谢!
cnhgj 2003-10-17
  • 打赏
  • 举报
回复
webRequest wr = webRequest("http://www.sina.com.cn");
webResponse wrp = wr.GetResponse();
StreamReader Sr = new StreamReader(wrp.GetResponseStream(),Encoding.GetEncoding("gb2312"));
Response.Write(Sr.ReadToEnd());
Lorenes 2003-10-17
  • 打赏
  • 举报
回复
如果你用WebRequest取 .Net 的站点, 别忘记挂上 CookieLess ID 才好用...

否则经常出错!!! 切记! 切记!
Lorenes 2003-10-17
  • 打赏
  • 举报
回复
是 Encoding 的问题,
而且C# Web 的类本来就不是很稳定.

源码都没敢公开.估计是太烂了.

关于这个问题, 先用 try{}catch(Exception){} 容错一下

然后再次请求.

甚至做个for 什么的.不信取不下来....呵呵...
saucer 2003-10-16
  • 打赏
  • 举报
回复
>>>有一些出入

try

System.IO.Stream stream=webrs.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream,System.Text.Encoding.GetEncoding ("GB2312"));
result = sr.ReadToEnd();
sr.Close();
stream.Close();


>>>>500服务器内部错误

from the remote site or your own site? give more details?

110,533

社区成员

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

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

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