簡體   English   中英

使用 HTTP WEB REQUEST 從 URL 讀取響應

[英]Reading Response From URL using HTTP WEB REQUEST

我必須閱讀來自http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA 的響應我使用了以下代碼,但沒有得到所有響應。 而不是它給出了錯誤頁面的響應提前致謝。

protected void Page_Load(object sender, EventArgs e)
{

    string sUrl = "http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA";
    XmlDocument rssDoc = new XmlDocument();
    XmlTextReader rssReader = new XmlTextReader(sUrl.ToString());

    WebRequest wrGETURL;
    wrGETURL = WebRequest.Create(sUrl);

    Stream objStream;
    objStream = wrGETURL.GetResponse().GetResponseStream();
    StreamReader objReader = new StreamReader(objStream, Encoding.UTF8);
    WebResponse wr = wrGETURL.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
    Response.Write(content);
   }

我不知道你想用XmlTextReader做什么,因為返回的內容是html而不是xml ,但是設置 UserAgent 足以獲取頁面。

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
using (var resp = req.GetResponse())
{
    var html = new StreamReader(resp.GetResponseStream()).ReadToEnd();
}
Stream objStream;
StreamReader objSR;
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

string str = "http://domaninname.com/YourPage.aspx?name=" + "abc";
HttpWebRequest wrquest = (HttpWebRequest)WebRequest.Create(str);
HttpWebResponse getresponse = null;
getresponse = (HttpWebResponse)wrquest.GetResponse();

objStream = getresponse.GetResponseStream();
objSR = new StreamReader(objStream, encode, true);
string strResponse = objSR.ReadToEnd();
Response.Write(strResponse);

在一行中,使用更簡單的 vb.net(獲得與 c# 相同的性能)

Return New System.IO.StreamReader(System.Net.HttpWebRequest.Create("http://urltogetthecontents.com").GetResponse().GetResponseStream()).ReadToEnd()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM