question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked TimonYang-MSFT answered

C# HttpClient - localhost works, IP address not

Hello,

localhost works, IP address not, why? How can I solve it?

  <appSettings>
     <add key="baseAddress" value="http://127.0.0.1:8888/" />    
  
     <!--<add key="baseAddress" value="http://localhost:8888/" />-->        
       
     <add key="url" value="" />
     
   </appSettings>  


  using (var request = new HttpRequestMessage(HttpMethod.Post, mesUri)) //$"{BaseAddress}{Url}"))
                     {
    
                         Log.Info($"[CUSTOM][PostBasis2], Adress= {requestUri}");
    
                         client.Timeout = TimeSpan.FromSeconds(3);
    
                         request.Version = HttpVersion.Version10; // Version11;
                         var json = JsonConvert.SerializeObject(content);
    
                         Log.Info($"[CUSTOM][PostBasis2], RequestMES=Object Post - Json\n\n{json}\n\n");
    
                         using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
                         {
                             request.Content = stringContent;
    
                             using (HttpResponseMessage response = client.PostAsync(requestUri, stringContent).Result)
                             {


I get an exception when I use the IP address. Why?

  client.PostAsync(requestUri, stringContent).Result


dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

And what exception would that be?

0 Votes 0 ·

ip-addresses-not-allowed-with-httpclientgetasync


Same no solution.

With local host all ok.

![97653--1a.png][3]


97617--1works-withlocalhost.png


2021-05-18-2105........WebException BB PostBasis2 = 'http://127.0.0.1:8888/'


Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


[3]: /answers/storage/attachments/97653--1a.png

0 Votes 0 ·
-1a.png (38.3 KiB)

II doubt that there is anything wrong with using an IP. If data is being passed and it's malformed that could be a possibility.

You can also use Fiddler an look at the URL request that got the HTTP 400 and see ii there is anything wrong with the URL.

0 Votes 0 ·
Show more comments

1 Answer

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered

Unexpected character encountered while parsing value: <. Path'', line 0, position 0.

This is an error when parsing Json, but it is caused by a data error in the response, so if we can solve the problem of Http error 400, this problem should disappear.

Please check this link below, this post seems to have encountered the exact same problem:

C# Web - localhost:port works, 127.0.0.1:port doesn't work

But it should be noted that we should preset that such a situation may be encountered and handle exceptions.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.