本文介绍了读取响应文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 WebHeaderCollection header= response.Headers;  

下面Google返回的文本。如何看呢?

Here google returns text. How to read it?

推荐答案

应用程序/ XRDS + XML是给我的问题,我收到了内容长度0(无应答)。

Your "application/xrds+xml" was giving me issues, I was receiving a Content-Length of 0 (no response).

移除后,您可以使用访问响应 response.GetResponseStream()

After removing that, you can access the response using 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();
        }

这篇关于读取响应文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:50