使用httpwebrequest获取响应并正确设置标头c#

时间:2013-07-31 18:49:31

标签: c# header httpwebrequest

我正在尝试获取网页{http {3}}的httpwebresponse我想设置httpwebrequest标头以接收正确的响应 当我使用firebug时,我可以看到请求GET financialHighlights?symbol = NOK

    Request Headers
    Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding gzip, deflate
    Accept-Language en-US,en;q=0.5
    Cache-Control   max-age=0
    Connection  keep-alive
    Cookie  info=edition=US; autorefresh=true; __csmv=8217fe64d605ff53; tns=dataSource=cookie&value=r%3AWMT.N%2Cr%3ANOK%2Cr%3AMXWL.O%2Cr%3AZNGA.O%2Cr%3ACL%2Cr%3AGWAY.N%2Cr%3AWAC; _tr_id.6e08dd17=a90fa05e29dee8e5.1372892223.15.1375294449.1375242766; rsi_segs=I07714_10493|I07714_10507|I07714_10383|I07714_10162|I07714_10173|I07714_10267|I07714_10272|I07714_10445|I07714_10484|I07714_10246|I07714_10495|I07714_10502|I07714_10077|I07714_10379|I07714_10381|I07714_10539|I07714_10540|I07714_0; WT_FPC=id=173.18.210.14-448669392.30308073:lv=1375312449650:ss=1375306512018; __utma=108768797.1221077780.1375239985.1375239985.1375288512.2; __utmz=108768797.1375239985.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _lsd0=d00be99d-db81-4118-ad9d-4979ac859ed7; __csv=52ebad372f495912|0; __qca=P0-1654114799-1372892258182; burt_test=1; richUserId=MQJ3UH6PVZDX; __csgeo=deg.16153_deg5.674_deg10.175_cc.US; __csnv=7c7a6d0ce0740439; cto_reuters=; _tr_ses.6e08dd17=1375288511678; _tr_cv.6e08dd17=false; __utmb=108768797.25.10.1375288512; __ctl=52ebad372f4959121; info=edition=US; __utmc=108768797; richSync=%7B%22burt%22%3A%22MQTBOU2R7GKV%22%7D; __csref=http%3A%2F%2Fwww.reuters.com%2Ffinance%2Fstocks%2Foverview%3Fsymbol%3DNOK; __cst=74e4787bfc3bc90f
    Host    www.reuters.com
    Referer http://www.reuters.com/finance/stocks/financialHighlights?symbol=NOK
    User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0

    Response Headers
    Connection  keep-alive
    Content-Length  78734
    Content-Type    text/html;charset=UTF-8
    Date    Wed, 31 Jul 2013 18:19:25 GMT
    Expires Wed, 31 Jul 2013 18:09:51 GMT
    Server  Apache
    Vary    Accept-Encoding

我得到了这个响应,并且在firebug中显示的响应不一样

这是我正在使用的cs文件     使用系统;     使用System.Text;     使用System.Net;     使用System.Web;     使用System.IO;

static class main {

    static void Main(String[] args) {
        HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://www.reuters.com/finance/stocks/financialHighlights?symbol=NOK");
        hwr.Method = "GET";
        hwr.CookieContainer = new CookieContainer();
        //hwr.CookieContainer = PersistentCookies.GetCookieContainerForUrl(url);
        hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        hwr.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        hwr.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
        String x = "info=edition=US; autorefresh=true; __csmv=8217fe64d605ff53; tns=dataSource=cookie&value=r%3AWMT.N%2Cr%3ANOK%2Cr%3AMXWL.O%2Cr%3AZNGA.O%2Cr%3";
        x += "ACL%2Cr%3AGWAY.N%2Cr%3AWAC; _tr_id.6e08dd17=a90fa05e29dee8e5.1372892223.15.1375290575.1375242766; rsi_segs=I07714_10383|I07714_10173|I07714_10267|";
        x += "I07714_10272|I07714_10445|I07714_10484|I07714_10379|I07714_10381|I07714_10540|I07714_0; WT_FPC=id=173.18.210.14-448669392.30308073:lv=1375308577125:";
        x += "ss=1375306512018; __utma=108768797.1221077780.1375239985.1375239985.1375288512.2; __utmz=108768797.1375239985.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd";
        x += "(none); _lsd0=d00be99d-db81-4118-ad9d-4979ac859ed7; __csv=52ebad372f495912|0; __qca=P0-1654114799-1372892258182; burt_test=1; richUserId=MQJ3UH6PVZDX;";
        x += " __csgeo=deg.16153_deg5.674_deg10.175_cc.US; __csnv=987c0e966f13095; cto_reuters=; info=edition=US; _tr_ses.6e08dd17=1375288511678; _tr_cv.6e08dd17=false;";
        x += " __utmb=108768797.11.10.1375288512; __utmc=108768797; __csref=http%3A%2F%2Fwww.reuters.com%2Ffinance%2Fstocks%2Foverview%3Fsymbol%3DNOK; __cst=19b4dea64115";
        x += "8295; __ctl=52ebad372f4959121";
        hwr.Headers.Add(HttpRequestHeader.Cookie, x);
        hwr.KeepAlive = true;
        hwr.Host = "www.reuters.com";
        hwr.Referer = "http://www.reuters.com/finance/stocks/financialHighlights?symbol=NOK";
        hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";


        HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
        Stream receiveStream = response.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        StreamReader readStream = new StreamReader(receiveStream, encode);

        String linesOfHTML = readStream.ReadToEnd();
        System.Console.WriteLine(linesOfHTML);
        System.Console.ReadKey();

        receiveStream.Close();
        response.Close();
        readStream.Close();

    }
}

httpwebreponse与我想要的html不一样,如何设置标题以接收正确的响应;

我得到的回应是html来源         www.reuters.com/finance/stocks/overview?symbol=NOK

但我想从中获取消息来源         www.reuters.com/finance/stocks/financialHighlights?symbol=NOK

0 个答案:

没有答案