none
Getting exception while requesting the API for the first time later on success RRS feed

  • Question

  • I'm getting the exception while Calling the api for the first time and later on it works here is the code i have done 

      private async Task GetRequest()
            {
                try
                {

                    httpClient = new HttpClient();
                    httpClient = CreateHttpClientAdapter(" http://routerlogin.com/cgi-bin/CF_logs.html", null, "application/json", null, HttpMethod.Get);
                    httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US");
                    httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
                    HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequest, HttpCompletionOption.ResponseContentRead);
                    if (httpResponseMessage != null)
                    {

                        if (httpResponseMessage.IsSuccessStatusCode)
                        {
                            //string responseResult = await httpResponseMessage.Content.ReadAsStringAsync();
                            var buffer = await httpResponseMessage.Content.ReadAsBufferAsync();
                            var byteArray = buffer.ToArray();
                            string responseResult = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

                        }

                    }


                }
                catch (Exception ex)
                {
                }
            }

            private HttpClient CreateHttpClientAdapter(string url, string body, string contentType, string para, HttpMethod method)
            {
                try
                {
                    httpClient = new HttpClient();

                    httpRequest = new HttpRequestMessage(method, new Uri(url));

                    httpRequest.Headers.Clear();

                     

                    httpRequest.Headers.Add("Accept", "application/json");


                    if (!string.IsNullOrWhiteSpace(body))
                    {
                        httpRequest.Content = new HttpStringContent(body, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                    }

                    if (!string.IsNullOrWhiteSpace(contentType) && httpRequest.Content != null)
                    {
                        httpRequest.Content.Headers.ContentType = new HttpMediaTypeHeaderValue(contentType);
                    }

                    return httpClient;
                }
                catch (Exception ex)
                {

                    return null;
                }
            }

    Exception Message : 

    The text associated with this error code could not be found.

    '': Invalid characters found.

    Exception StackTrace :

       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at HtmlTextRendering.MainPage.<GetRequest>d__4.MoveNext()

    i have console applications it is working fine only happening in UWP . I have tried in POSTMAN also it is success 





    Friday, June 7, 2019 12:35 PM

All replies

  • Hi,

    I made a test for your code snippet but I could not get the problem you said. I could successfully send the request even for the first time.

    I have some questions here. Does this issue happens every time or it happens randomly?  Could you please share me your OS version of your device and target version of your app?  Because I could not reproduce your problem, please try to narrow down the problem when you debug this method, find out which line of the code gives the error. This is helpful for us to find out what is wrong.

    Best regards,

    Roy


    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.

    Monday, June 10, 2019 6:57 AM
    Moderator
  • It Happens Randomly not all the times 

    Target version:  Windows 10 Fall Creators Update (10.0; Build 16299)

    Min version:Windows 10 Anniversary Update (10.0; Build 14393)

    Os Version i'm using is 

    Version OS Build Windows 10 Enterprise 1709 16299.1146

      HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequest, HttpCompletionOption.ResponseContentRead);

    at this line while sending request getting the error

    Thanks,

    Harish

    Monday, June 10, 2019 11:09 AM
  • Hi,

    Well, it's strange. We need to see the difference between the good request and the bad request. I suggest you could catch the succeed request. Checked the parameters and the URL. Then when you get the exception, check them in the bad request as well. 

    Best regards,

    Roy


    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.

    Tuesday, June 11, 2019 9:34 AM
    Moderator
  • I have checked the requests for first time and second time also both are same and i'm able to reproduce this issue . 

    Thursday, June 13, 2019 1:44 PM
  • Hi,

    Since I could not reproduce your issue, I'll ask another engineer to take a look at this issue. There might be some time delay. Thank you for your patience.

    Best regards,

    Roy


    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.

    Friday, June 14, 2019 3:07 AM
    Moderator
  • Hi Roy Li,

    For us it is priority one we are not able to send the basic request . Help us on this as soon as possible

    Friday, June 14, 2019 6:54 AM
  • Hi,

    I fully understand your feelings. I had reported this issue via our internal channel. I'm also wait for the response from the team. As a workaround, I suggest that you could handle the exception when it gives error for the first time, then re-send it as it works correctly in the second time.

    Best regards,

    Roy


    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.

    Monday, June 17, 2019 2:38 AM
    Moderator
  • Hi Roy,

    The issue is now always reproducible can you please try from your side and let us know if you are able to reproduce or not

    Thanks,

    Harish

    Monday, June 17, 2019 6:46 AM
  • Hi,

    I test it again but still could not reproduce this using your code snippet. Since I had report this to the team, I'm waiting for the response from the team. You could use the workaround I mentioned in my last reply. 

    Best regards,

    Roy


    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.

    Tuesday, June 18, 2019 1:32 AM
    Moderator
  • Hi Roy ,

    We cannot make workaround here because there are more than 100 api calls based on this . So all the calls will be duplicate in this case.Is there any other way to show the exeception then you can have look into it

    Thanks,

    Harish

    Tuesday, June 18, 2019 5:23 AM

  • I have implemented a console application to find the exact error while sending the request 

      public static async Task httpClientRequest()
            {
                try
                {
                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "B3aRj2qLnDBBPRL7ZUzHhGC0witk");
                    var res = await client.GetStringAsync("https://mingle-ci01-ionapi.mingle.awsdev.infor.com/MNGQACIMOBILE_AX2/minglechat/api/v1/user/profile");
                }
                catch (Exception ex)
                {

                }
            }



    I got error as "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF"  based on this error i have searched in and 

    i followed in console app in app.config file i have inserted the below code 

    <system.net>
        <settings>
          <httpWebRequest useUnsafeHeaderParsing="true" />
        </settings>
      </system.net>

    then the request is success , but how to implement this in my "UWP APP" since i don't app.config file in project . I tried to insert the above code in app.manifeast file but no use . can you help us on this

    Thanks,

    Harish
    Tuesday, June 18, 2019 8:37 AM
  • Hi,

    I'm asking another engineer to take a look at this issue. There might be some time delay. Thank you for your patience.

    Best regards,

    Roy


    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.

    Monday, June 24, 2019 3:30 AM
    Moderator
  • Hi Roy ,

    Still now no one has contacted me regarding this issue . This Issue pending from longback .



    Thursday, July 11, 2019 10:26 AM