C# - HTTPWebRequest 不返回任何内容



所以如果没有打开小提琴手,我的代码似乎什么也没做!但是打开它,它工作正常?!什么?

HttpWebRequest init = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/en/login?continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F");
init.Method = "GET";
init.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
init.Timeout = 8000;
HttpWebResponse resp1 = (HttpWebResponse)init.GetResponse();

线程(在我的例子中是 ThreadPool 线程(只会冻结,而不是结束,只会冻结,因为它无法连接,并且 try catch 似乎也不起作用。

如果我将 Fiddlers 设置上的端口更改为 8888 以外的任何内容并打开 Fiddler,它会再次开始冻结,这意味着它以某种方式链接到 8888。然而,它根本没有在我的代码中引用。


固定!
发现是没关门造成的! resp1.close() .谢谢大家!

这是正确的,你在代码中写的东西。尝试调试此代码,看看你得到什么作为响应,注意响应正文HtmlResult

HttpWebRequest init = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/en/login?continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F");
init.Method = "GET";
init.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
init.Timeout = 8000;
HttpWebResponse resp1 = (HttpWebResponse)init.GetResponse();
HttpStatusCode sc = resp1.StatusCode;
String sd = resp1.StatusDescription;
string HtmlResult = "";
string responseBody = "";
using (System.IO.Stream rspStm = resp1.GetResponseStream())
{
    using (System.IO.StreamReader reader = new System.IO.StreamReader(rspStm))
    {
        HtmlResult = HtmlResult +
                "Response Description: " + resp1.StatusDescription;
        HtmlResult = HtmlResult +
                "Response Status Code: " + resp1.StatusCode;
        HtmlResult = HtmlResult + "rnrn";
        responseBody = reader.ReadToEnd();
    }
}

相关内容

  • 没有找到相关文章

最新更新