阅读回复中的文字

时间:2010-07-17 20:45:17

标签: c# asp.net

HttpWebRequest request = WebRequest.Create("http://google.com") as HttpWebRequest;  

request.Accept = "application/xrds+xml";  
HttpWebResponse response = (HttpWebResponse)request.GetResponse();  

WebHeaderCollection header = response.Headers;

这里google返回文字。如何阅读?

7 个答案:

答案 0 :(得分:96)

您的"application/xrds+xml"给了我一些问题,我收到的内容长度为0(无回复)。

删除后,您可以使用response.GetResponseStream()访问回复。

HttpWebRequest request = WebRequest.Create("http://google.com") as HttpWebRequest;

//request.Accept = "application/xrds+xml";  
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

WebHeaderCollection header = response.Headers;

var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
    string responseText = reader.ReadToEnd();
}

答案 1 :(得分:66)

The accepted answer未正确处理WebResponse或解码文本。此外,还有一种在.NET 4.5中执行此操作的新方法。

要执行HTTP GET并阅读响应文本,请执行以下操作。

.NET 1.1 - 4.0

public static string GetResponseText(string address)
{
    var request = (HttpWebRequest)WebRequest.Create(address);

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        var encoding = Encoding.GetEncoding(response.CharacterSet);

        using (var responseStream = response.GetResponseStream())
        using (var reader = new StreamReader(responseStream, encoding))
            return reader.ReadToEnd();
    }
}

.NET 4.5

private static readonly HttpClient httpClient = new HttpClient();

public static async Task<string> GetResponseText(string address)
{
    return await httpClient.GetStringAsync(address);
}

答案 2 :(得分:10)

我自己就试过了,它给了我一个200 OK的回复,但没有内容 - 内容长度为0.你确定它给你的内容吗?无论如何,我会假设你真的有内容。

获取实际文本依赖于知道编码,这可能很棘手。它应该在Content-Type标题中,但是你必须解析它等等。

但是,如果这是实际 XML(例如来自“http://google.com/xrds/xrds.xml”),那么它就容易多了。只需将XML加载到内存中,例如通过LINQ to XML。例如:

using System;
using System.IO;
using System.Net;
using System.Xml.Linq;
using System.Web;

class Test
{
    static void Main()
    {
        string url = "http://google.com/xrds/xrds.xml";
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

        XDocument doc;
        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                doc = XDocument.Load(stream);
            }
        }
        // Now do whatever you want with doc here
        Console.WriteLine(doc);
    }   
}

如果内容是XML,则将结果转换为XML对象模型(无论是XDocumentXmlDocument还是XmlReader)可能比使用纯文本更有价值。

答案 3 :(得分:2)

本文概述了使用HttpWebResponse对象:How to use HttpWebResponse

以下相关位:

HttpWebResponse webresponse;

webresponse = (HttpWebResponse)webrequest.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(),enc);

string Response = loResponseStream.ReadToEnd();

loResponseStream.Close();
webresponse.Close();

return Response;

答案 4 :(得分:2)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();

答案 5 :(得分:1)

应该使用

response.GetResponseStream()来返回响应流。并且不要忘记close StreamResponse个对象。

答案 6 :(得分:0)

如果您的http请求是Post并且request.Accept = "application/x-www-form-urlencoded"; 那么我认为您可以通过以下代码获得respone的文本:

var contentEncoding = response.Headers["content-encoding"];
                        if (contentEncoding != null && contentEncoding.Contains("gzip")) // cause httphandler only request gzip
                        {
                            // using gzip stream reader
                            using (var responseStreamReader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)))
                            {
                                strResponse = responseStreamReader.ReadToEnd();
                            }
                        }
                        else
                        {
                            // using ordinary stream reader
                            using (var responseStreamReader = new StreamReader(response.GetResponseStream()))
                            {
                                strResponse = responseStreamReader.ReadToEnd();
                            }
                        }