none
How to use proxy in Http client RRS feed

  • Question

  • Hello:

    I recently subscribed a paid proxy service, it works well.  Since I am using a fixed IP address, the proxy service can authenticate me via my IP address, so I don't have to input user name, password to get through.

    However, I can't find any good C# code sample on how to create a httpclient using proxy without user name/password.

    All the examples I saw needs user name/password.

    I have the following C# code:

    public static async Task<string> Http1_Get_With_Proxy(string url1)
    {
        string html_content = "";
        try
        {
        WebProxy proxy = new WebProxy
        {
        Address = new Uri($"1.2.3.4:8888"),
        };
        ServicePointManager.Expect100Continue = false;
        ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        HttpClientHandler clientHandler = new HttpClientHandler()
        {
        AllowAutoRedirect = true,
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
        Proxy = proxy,
        };
        using HttpClient client1 = new HttpClient(clientHandler);
        client1.DefaultRequestHeaders.Accept.Clear();
        client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
        client1.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url1);
        HttpResponseMessage response = await client1.SendAsync(request);
        if (response.StatusCode == HttpStatusCode.OK)
        {
        html_content = await response.Content.ReadAsStringAsync();
        }
        }
        catch (HttpRequestException ex)
        {
        Console.WriteLine(ex.Message);
        }
        return (html_content);
    }
    

    When I ran my code, I got error message:

    {"Invalid URI: The URI scheme is not valid."}

    I can understand the error, since pure IP address is not a valid URI, but how I can do this?  I just want to use pure IP:Port# without http://, as I don't need them for the proxy service.

    I even want one step further: how I can do this for HTTP2 client?

    By the way, I am using Visual Studio 2019 Version 16.3.3 on Windows 10 (1903).

    Please advice!

    Tuesday, October 8, 2019 10:00 PM

Answers

  • Hello:

    Actually, I figured out not long after I posted the question.  It was rather simple, simply change one statement.

    WebProxy proxy = new WebProxy
        {
        Address = new Uri($"http://1.2.3.4:8888"),
        };

    Then it works!

    Wednesday, October 9, 2019 8:00 AM

All replies

  • Hi zydjohn,

    Thank you for posting here.

    For your question, you want to know how to create a httpclient using proxy with user name and password.

    You could refer to the following link.

    c# HttpClient with proxy

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 9, 2019 2:59 AM
    Moderator
  • Hello:

    Actually, I figured out not long after I posted the question.  It was rather simple, simply change one statement.

    WebProxy proxy = new WebProxy
        {
        Address = new Uri($"http://1.2.3.4:8888"),
        };

    Then it works!

    Wednesday, October 9, 2019 8:00 AM