request.GetResponse()失败 [英] request.GetResponse() Fails
问题描述
我遇到GetResponse()调用问题。我在http编程中知之甚少,也许它可以用来工作,如果有的话,请为我大声说出来。
I have problem with the GetResponse() call. I have little knowlege in http programming and maybe it isent suppused to work, if so, pls shout it out for me.
我试图从一个特殊的http地址获取HTML,我已经在另一个html页面执行了这个任务,它运行得很好。但是当尝试使用其他页面时,它会失败。没有提出execption但是contentlength = -1。
Im trying to get the HTML from a paticular http adress, I have already performed this task at another html page and it works perfectly. But when trying with another page it fails. No execption is raised but contentlength = -1.
2个html页面之间的一个区别是失败的那个包含图片ref也是一个问题?
One differnence between the 2 html pages are that the one who fails contains a picture ref also, is that a problem?
失败的HTML页面
http://212.190.114.198/scripts/mgrqispi.dll?APPNAME=Visveiling&PRGNAME=FSTATMARKET&ARGUMENTS=-AE
http://212.190.114.198/scripts/mgrqispi.dll?APPNAME=Visveiling&PRGNAME=FSTATMARKET&ARGUMENTS=-AE
我用uri调用GetUTF8WebPage down belov,它失败
Im calling GetUTF8WebPage down belov with the uri and it fails
感谢所有帮助。
public static String GetUTF8WebPage( String uri)
public static String GetUTF8WebPage(String uri)
{
尝试
try
{
HttpWebRequest request =( HttpWebRequest ) WebRequest .Create( uri);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
System.Net。 WebResponse response = request.GetResponse ();在这里它
System.Net.WebResponse response = request.GetResponse(); Here it failes
使用 (System.IO。 Stream stream = response.GetResponseStream())
using (System.IO.Stream stream = response.GetResponseStream())
{
Int32 length =( Int32 )response.ContentLength;
Int32 length = (Int32)response.ContentLength;
Byte [] buffer = new 字节 [length];
Byte[] buffer = new Byte[length];
stream.Read(缓冲区,0,长度);
stream.Read(buffer, 0, length);
string st = System.Text。 UTF8Encoding .UTF8.GetString(buffer);
string st = System.Text.UTF8Encoding.UTF8.GetString(buffer);
return < font size = 2> st;
return st;
}
}
catch
catch (Exception ex)
{
控制台 .WriteLine(ex.Message);
Console.WriteLine(ex.Message);
}
return "" ;
return "";
}
推荐答案
在您调用响应之前添加以下行:
request.Method =" GET";
request.AddRange(0); //这将迫使服务器向我们发送ContentLenght(也可以使用Int64而不是Int32)
我希望这可以解决您的问题。
before you call response add this lines:
request.Method="GET";
request.AddRange(0); // this will force the server to send us back ContentLenght (also you can use Int64 instead Int32)
I hope this will solve your problem.
这篇关于request.GetResponse()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!