只是一段代码
WebClient wc = new WebClient();
String str = wc.DownloadString(new Uri("http://content.warframe.com/dynamic/rss.php"));
我得到了例外:
发生了'System.Net.WebException'类型的未处理异常 System.dll中
其他信息:基础连接已关闭: 连接意外关闭。
我认为这是.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
上面的代码也不起作用,两者都会抛出相同的异常
Fiddler日志:
会话状态:已中止。
响应实体大小:512字节。
== FLAGS ==================
BitFlags:[ResponseGeneratedByFiddler] 0x100
X-ABORTED-WHEN:完成
X-CLIENTIP:127.0.0.1
X-CLIENTPORT:2747
X-EGRESSPORT:2748
X-FAILSESSION-WHEN:ReadingResponse
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查询:164ms
TCP / IP连接:74ms
HTTPS握手:0ms
ServerConnected: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 CACHE INFO ============
WinINET缓存中不存在此URL。 [代码:2]
*注意:上面的数据显示了WinINET当前的缓存状态,而不是请求时的状态
*注意:上面的数据仅显示WinINET的中等完整性(非保护模式)缓存。
另外 - 504,这没有意义,因为我可以通过chrome / firefox / ie ...从链接获取数据
我只是用其他语言工作,但是我被迫用C#做了(我已经制作了2个代码来重写它)
我添加了一些像fiddler所说的设置
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";
但现在输出混乱且不可读
答案 0 :(得分: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";
答案 1 :(得分:2)
检查这个答案:
..The underlying connection was closed: An unexpected error occurred on a receive
所以这可能适合你:
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();
}
答案 2 :(得分: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 方法外,我还要添加 decompresion 行
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
感谢您的提示(特别是小提琴,Decode按钮节省了我的时间来找出错误的内容)