首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

winform发送http请求解决思路

2012-09-07 
winform发送http请求在winform里我想发送http请求,并得到返回内容怎么做比如有个URL为http://localhost:80

winform发送http请求
在winform里我想发送http请求,并得到返回内容怎么做
比如有个URL为http://localhost:8088/a.aspx?id=2,返回内容为html
我用webbrowser.navigate或webbrowser.url执行,然后得到的webbrowser.documenttext为空,还有其他方法没得

[解决办法]
调用 getPage("http://localhost:8088/a.aspx?id=2");
public void getPage(String url) 
{
WebResponse result = null;

try 
{
WebRequest req = WebRequest.Create(url);
req.Proxy = null;
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();

//read the stream into a string
StreamReader sr = new StreamReader( ReceiveStream );
string resultstring = sr.ReadToEnd();

Console.WriteLine("\r\nResponse stream received");
Console.WriteLine(resultstring);

catch(Exception exp) 
{
Console.Write("\r\nRequest failed. Reason:");
Console.WriteLine(exp.Message);

finally 
{
if ( result != null ) 
{
result.Close();
}
}

Console.WriteLine("\r\nPress Enter to exit.");
Console.ReadLine();
}

热点排行