我在浏览器的URL中输入:http://www.ahlawyer.com/search/searchpage.php?start=25&page=2&BeginYear=1949&EndYear=2006&databaseid=8&Type=有效&Title=&area=不限
可以查得到结果,而如果我用WebRequest.Create(url)方法提交,却查不出来结果?我用抓包工具发现我的Get请求中中文的参数已经变成了乱码。
请教高手:这是为什么?
我的代码如下:
rl="http://www.ahlawyer.com/search/searchpage.php?start=25&page=2&BeginYear=1949&EndYear=2006&databaseid=8&Type=有效&Title=&area=不限";
string enco = "gb2312";
referer="http://www.ahlawyer.com/search/search.php?databaseid=8&Title=&area=%B2%BB%CF%DE&BeginYear=1949&EndYear=2006&Type=%D3%D0%D0%A7&Submit=+%BC%EC%CB%F7+";
HttpWebResponse result = null;
StringBuilder responseBuffer =new StringBuilder();
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Referer=referer;
result = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(enco);
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\nResponse stream received");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
Console.Write(str);
responseBuffer.Append(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine(""); return responseBuffer.ToString();
}
catch(Exception e)
{
ApplicationLogCls.Log("The request URI could not be found or was malformed");
ApplicationLogCls.Log( e.ToString());
Console.WriteLine( e.ToString());
Console.WriteLine("\r\nThe request URI could not be found or was malformed");
return null;
}
可以查得到结果,而如果我用WebRequest.Create(url)方法提交,却查不出来结果?我用抓包工具发现我的Get请求中中文的参数已经变成了乱码。
请教高手:这是为什么?
我的代码如下:
rl="http://www.ahlawyer.com/search/searchpage.php?start=25&page=2&BeginYear=1949&EndYear=2006&databaseid=8&Type=有效&Title=&area=不限";
string enco = "gb2312";
referer="http://www.ahlawyer.com/search/search.php?databaseid=8&Title=&area=%B2%BB%CF%DE&BeginYear=1949&EndYear=2006&Type=%D3%D0%D0%A7&Submit=+%BC%EC%CB%F7+";
HttpWebResponse result = null;
StringBuilder responseBuffer =new StringBuilder();
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Referer=referer;
result = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(enco);
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\nResponse stream received");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
Console.Write(str);
responseBuffer.Append(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine(""); return responseBuffer.ToString();
}
catch(Exception e)
{
ApplicationLogCls.Log("The request URI could not be found or was malformed");
ApplicationLogCls.Log( e.ToString());
Console.WriteLine( e.ToString());
Console.WriteLine("\r\nThe request URI could not be found or was malformed");
return null;
}
url=System.Web.HttpUtility.UrlEncode(url,enco);
url=System.Web.HttpUtility.UrlEncode(url,enco);