语言

Menu
Sites
Language
HttpClient post exeption

Hi. I have tried to call post request  by HttpClient and got exception ""Couldn't resolve host name""  This happens only on real device on emulator all is ok. 

I write for tizen os at first time and don't know what could be happened. 
Privileges were added.
 

Tizen version "4.0"; Device "Galaxy Watch SM-R810" 

 

HttpClient httpClient = new HttpClient();
            string data = null;
            try
            {
                httpClient.DefaultRequestHeaders
                      .Accept
                      .Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.Timeout = new TimeSpan(0, 1, 0);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "RequsetUrl");
                var requestContent = new StringContent(content,
                                                    Encoding.UTF8,
                                                    "application/json");//CONTEN
                var result = await httpClient.PostAsync(url, requestContent);
                data = await result.Content.ReadAsStringAsync();
                Console.WriteLine("\n\n  GetDataFromWeb() Exception Caught!");
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("\n\n  GetDataFromWeb() Exception Caught!");
                Console.WriteLine("Message: {0}, StackTrace: {1}, InnerException: {2}", ex.Message, ex.StackTrace, ex.InnerException.Message);
            }
            return data;

编辑者为: Fedir Klymenko 03 8月, 2020

响应

1 回复
Tizen .NET

Hi, with WiFi connection on SM-R800, the below code works.

Please check it again.

        private async void testHTTPClient()
        {
            HttpClient httpClient = new HttpClient();
            string data = null;
            string url = "http://google.com";
            try
            {
                httpClient.DefaultRequestHeaders
                    .Accept
                    .Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.Timeout = new TimeSpan(0, 1, 0);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "RequsetUrl");
                var requestContent = new StringContent("Content", Encoding.UTF8, "application/json");
                var result = await httpClient.PostAsync(url, requestContent);
                data = await result.Content.ReadAsStringAsync();
                Console.WriteLine("\n\n data [ {0} ]", data);
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("\n\n  testHTTPClient() HttpRequestException occurs");
                Console.WriteLine("Message: {0}, StackTrace: {1}, InnerException: {2}",
                    ex.Message, ex.StackTrace, ex.InnerException.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\n  testHTTPClient() Exception occurs!");
                Console.WriteLine("Type: {0}, Message: {1}, StackTrace: {2}, InnerException: {3}",
                    ex.GetType(), ex.Message, ex.StackTrace, ex.InnerException.Message);
            }
            
            return;
        }
I/DOTNET_LAUNCHER( 5354):  data [ <!DOCTYPE html>
I/DOTNET_LAUNCHER( 5354): <html lang=en>
I/DOTNET_LAUNCHER( 5354):   <meta charset=utf-8>
I/DOTNET_LAUNCHER( 5354):   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
I/DOTNET_LAUNCHER( 5354):   <title>Error 405 (Method Not Allowed)!!1</title>
I/DOTNET_LAUNCHER( 5354):   <style>
I/DOTNET_LAUNCHER( 5354):     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.goog
I/DOTNET_LAUNCHER( 5354): le.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
I/DOTNET_LAUNCHER( 5354):   </style>
I/DOTNET_LAUNCHER( 5354):   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
I/DOTNET_LAUNCHER( 5354):   <p><b>405.</b> <ins>That’s an error.</ins>
I/DOTNET_LAUNCHER( 5354):   <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>.  <ins>That’s all we know.</ins>
I/DOTNET_LAUNCHER( 5354):  ]