2

只是一段代码

WebClient wc = new WebClient();
String str = wc.DownloadString(new Uri("http://content.warframe.com/dynamic/rss.php"));

我得到了例外:

System.dll 中出现“System.Net.WebException”类型的未处理异常

附加信息:基础连接已关闭:连接意外关闭。

我认为这是 .NET 中的一个错误(我使用的是 3.5),但我尝试了其他方法来获取此链接的内容(它的 rss、xml)。还没有幸运的镜头

var webrequest = (WebRequest)HttpWebRequest.Create(@"http://content.warframe.com/dynamic/rss.php");
var resp = webrequest.GetResponse();
//HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); // Wont work also

上面的代码也不起作用,两者都抛出相同的异常

提琴手日志:

会话状态:中止。
响应实体大小:512 字节。

== FLAGS ==================
BitFlags:[ResponseGeneratedByFiddler] 0x100
X-ABORTED-WHEN:完成
X-CLIENTIP:127.0.0.1
X-CLIENTPORT:2747
X-EGRESSPORT: 2748
X-FAILSESSION-WHEN:阅读响应
X-HOSTIP:205.185.216.10
X-PROCESSINFO:willitwork.vshost:3300

== 时间信息 ============
ClientConnected: 10:29:11.706
ClientBeginRequest: 10:29:11.713
GotRequestHeaders: 10:29:11.713
ClientDoneRequest: 10:29:11.713
确定网关: 0ms
DNS 查找:164 毫秒
TCP/IP 连接:74 毫秒
HTTPS 握手: 0 毫秒服务器连接
:10:29:11.953
FiddlerBeginRequest:10:29:11.953
ServerGotRequest:10:29:11.953
ServerBeginResponse:10:29:12.372
GotResponseHeaders:00:00:00.000
ServerDoneResponse:10 :29:12.372
ClientBeginResponse: 10:29:12.385
ClientDoneResponse: 10:29:12.385

总耗时:0:00:00.672

响应在交付给客户端之前被缓冲。

== WININET 缓存信息 ============
此 URL 不存在于 WinINET 缓存中。[代码:2]
*注意:以上数据显示的是WinINET当前的缓存状态,而不是请求时的状态。
* 注意:以上数据仅显示 WinINET 的中等完整性(非保护模式)缓存​​。

另外-504,这没有意义,因为我可以通过chrome / firefox / ie从链接获取数据...

我只是用其他语言来工作,但我不得不用 C# 来做(我已经做了 2 很多代码来重写它)

我添加了一些设置,比如提琴手说

myHttpWebRequest1.ProtocolVersion = HttpVersion.Version11;
myHttpWebRequest1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
myHttpWebRequest1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

至少现在我得到 504 错误而不是“未知”,但我仍然可以通过 webbrowser 查看内容,所以 504 错误是假的

编辑:我添加时没有响应错误

myHttpWebRequest1.Headers["Accept-Encoding"] = "gzip";

但现在输出混乱且不可读

4

3 回答 3

3

我有同样的错误。

您可以将用户代理添加到您的httpRequest

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
于 2015-06-07T08:17:00.643 回答
2

好的,我得到了所有修复和工作!

static void Main(string[] args)
{
    Uri url = new Uri(@"http://content.warframe.com/dynamic/rss.php");

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    // MAGIC LINE GOES HERE \/
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

    // Assign the response object of HttpWebRequest to a HttpWebResponse variable.
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream streamResponse = response.GetResponseStream())
        {
            using (StreamReader streamRead = new StreamReader(streamResponse))
            {
                Char[] readBuff = new Char[2000];
                int count = streamRead.Read(readBuff, 0, 2000);
                while (count > 0)
                {
                    String outputData = new String(readBuff, 0, count);
                    Console.Write(outputData);
                    count = streamRead.Read(readBuff, 0, 2000);
                }
            }
        }
    }

    Console.ReadKey();
}

除了不使用WebClient.DownloadString方法外,我还必须添加解压缩

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

感谢您的提示(尤其是提琴手一号,解码按钮节省了我查找问题的时间)

于 2014-07-14T04:50:45.690 回答
2

检查这个答案:

..底层连接已关闭:接收时发生意外错误

所以这可能对你有用:

var webRequest = (HttpWebRequest)HttpWebRequest.Create(@"http://content.warframe.com/dynamic/rss.php");
webRequest.KeepAlive = false;
var resp = webRequest.GetResponse();        

编辑:

你是对的,检查一下:http: //msdn.microsoft.com/cs-cz/library/system.net.httpwebrequest.keepalive%28v=vs.110%29.aspx

这是将打印出收到的响应内容的工作代码:

    static void Main(string[] args)
    {
          // Create a new HttpWebRequest object.Make sure that 
          // a default proxy is set if you are behind a firewall.
          HttpWebRequest myHttpWebRequest1 =
            (HttpWebRequest)WebRequest.Create(@"http://content.warframe.com/dynamic/rss.php");

          myHttpWebRequest1.KeepAlive=false;
          // Assign the response object of HttpWebRequest to a HttpWebResponse variable.
          HttpWebResponse myHttpWebResponse1 = 
            (HttpWebResponse)myHttpWebRequest1.GetResponse();

          Console.WriteLine("\nThe HTTP request Headers for the first request are: \n{0}", myHttpWebRequest1.Headers);

          Stream streamResponse = myHttpWebResponse1.GetResponseStream();
          StreamReader streamRead = new StreamReader(streamResponse);
          Char[] readBuff = new Char[256];
          int count = streamRead.Read(readBuff, 0, 256);
          Console.WriteLine("The contents of the Html page are.......\n");
          while (count > 0)
          {
              String outputData = new String(readBuff, 0, count);
              Console.Write(outputData);
              count = streamRead.Read(readBuff, 0, 256);
          }
          Console.WriteLine();
          // Close the Stream object.
          streamResponse.Close();
          streamRead.Close();
          Console.ReadKey();
    }
于 2014-07-13T05:58:36.543 回答