简体   繁体   中英

.NET How to Send HttpClient Get Request on Specified IP address in same domain

I have a problem. I migrate from WebRequest to HttpClient. In WebRequest i simple implement:

var webRequest =   (HttpWebRequest)WebRequest.Create("http://111.111.111.111:8080/method");
webRequest.Host = "mysite:8080.com";

And http requests works fine.

Then i replace WebRequest on HttpClient, and faced with the problem.

var client = new HttpClient();
client.DefaultRequestHeaders.Host = "mysite:8080.com";
var result = await client.GetAsync("http://111.111.111.111:8080/method", cancellationToken).ConfigureAwait(false); // rise 404 error

GetAsync rise 404 in this case. HttpClient can't send requests on specified ip's?

This might be old, but to those who can bump in this error, here you can use this method :

static async void getHttpClient()
    {
        string mUrl = "http://192.168.1.111/method";
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(mURL))
        using (HttpContent content = response.Content)
        {
            string result = await content.ReadAsStringAsync();

            if (result != null)
            {
                Console.WriteLine(result);
            } else
            {
                 //
                 // ERROR
                 // 
            }
        }
    }

happy codings

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