简体   繁体   中英

C# - HTTPWebRequest not returning anything

So without fiddler open, my code doesnt seem to do ANYTHING! But with it open, it works fine?! What?

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();

The thread (In my case ThreadPool thread), will just freeze, not end, will just freeze as its not able to connect, and the try catch doesnt even seem to work either.

If I change the Port on Fiddlers Settings to anything other than 8888 and have Fiddler open, it starts to freeze again, meaning somewhere its linked to 8888 somehow. Yet its not referenced in my code at all.


FIXED!
Found out it was caused by not closing! resp1.close() . Thanks everyone!

It's done right that, what you wrote in your code. Try debug this code and see what you get as a response, pay attention to responseBody and 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 + "\r\n\r\n";
        responseBody = reader.ReadToEnd();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM