簡體   English   中英

具有網絡憑據的HttpWebResponse和代理不起作用

[英]HttpWebResponse and Proxy w/ NetworkCredentials Not Working

我正在一個使用代理獲取網站HTML代碼的項目。

現在,我遇到的麻煩是,我想在連接到代理時使用用戶名密碼驗證而不是IP-Auth。

我已經編寫了一個示例代碼,並與Snippy一起運行了它。 有效。 然后,我將相同的代碼復制到Visual Studio .NET 4.5項目中,但失敗並顯示以下錯誤:嘗試獲取響應時An existing connection was forcibly closed by the remote host

這是代碼:

    WebProxy[] proxies = { new WebProxy("ip", port) };
    proxies[0].Credentials = new NetworkCredential { UserName = "username", Password = "password" };

    string url = "https://www.google.com/search?q=s&num=50";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Proxy = proxies[0];

    using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
    {
        using (StreamReader response_stream = new StreamReader(res.GetResponseStream()) 
        {
            string html = response_stream.ReadToEnd();
            Console.WriteLine(html);
        }
    }

我嘗試了不同的變化。 當我切換到IP授權時,添加我的並注釋掉NetworkCredentials分配,該代碼在Snippy和Visual Studio中都可以正常工作。

但是,為什么在使用NetworkCredentials時失敗?

好,我找到了罪魁禍首。 那是我的代理人。

請注意在使用HttpWebRequest測試代理連接時遇到問題的每個人- 嘗試其他賣家 它可以節省您幾分鍾的時間。 或幾個小時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM