只是一段代码
WebClient wc = new WebClient();
String str = wc.DownloadString(new Uri("http://content.warframe.com/dynamic/rss.php"));
我得到exception:
System.Net类型的未处理异常。webeexception '发生在System.dll
附加信息:底层连接已关闭连接被意外关闭。
我知道这是。net中的一个bug(我使用的是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
上面的代码也不能工作,两者都强制转换相同的异常
提琴手日志:
会话状态:Aborted.
响应实体大小: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
== TIMING INFO ============
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 ============
此URL不存在于WinINET缓存中。(代码:2]
*注意:上面的数据显示的是WinINET当前的缓存状态,而不是请求时的状态。*注:以上数据仅显示WinINET的介质完整性(非保护模式)缓存。
也- 504,这是没有意义的,因为我可以从链接通过chrome/firefox/ie…
我只是为了在其他语言中工作而这样做,但我被迫用c#来做(我写了2个代码来重写它)
我添加了一些设置,比如fiddler said
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错误而不是"未知",但我仍然可以通过web浏览器查看内容,所以504错误是假
编辑:当我添加 时没有响应错误myHttpWebRequest1.Headers["Accept-Encoding"] = "gzip";
但现在输出是混乱的,不可读
我也有同样的错误。
你可以添加用户代理到你的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";
好的,我得到了这一切修复&工作!
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。下载字符串方法我必须添加解压行
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
感谢提示(特别是小提琴手之一,解码按钮节省了我的时间来找到什么是错的)
检查这个答案:
. .底层连接已关闭:在接收
上发生意外错误所以这可能对你有用:
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();
}