Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Can no longer get response from HttpWebRequest when 400 returned at Unity 2020,2021

Discussion in 'Scripting' started by gguolinegames, Jul 16, 2021.

  1. gguolinegames

    gguolinegames

    Joined:
    Mar 7, 2019
    Posts:
    8
    I recently upgraded Unity Version to 2020.3.14f1
    then my web request routine cannot get webrequest's response body when request code is 400 (BadRequest)

    It was working fine with Unity 2018,Unity 2019.
    but not working with Unity 2020.3.0f1,2020.3.14f1,2021.1.15f1

    It crashed when I read WebException's response stream.

    Code (CSharp):
    1.  IAsyncResult asyncResult = httpWebRequest.BeginGetResponse( new AsyncCallback( (IAsyncResult iarres) => {
    2.                 string resultText = "";
    3.                 System.Net.HttpStatusCode code = 0;
    4.                 try
    5.                 {                
    6.                     using ( System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)httpWebRequest.EndGetResponse( iarres ) )
    7.                     {
    8.                         using ( System.IO.StreamReader streamReader = new System.IO.StreamReader( response.GetResponseStream() ) )
    9.                         {
    10.                             resultText = streamReader.ReadToEnd();
    11.                         }
    12.                         code = response.StatusCode;              
    13.                         response.Close();
    14.                     }
    15.                 }
    16.                 catch ( WebException ex )
    17.                 {                
    18.                     if ( ex.Status == WebExceptionStatus.ProtocolError )
    19.                     {              
    20.                         using ( var stream = ex.Response.GetResponseStream() )
    21.                         {                  
    22.                             using ( System.IO.StreamReader streamReader = new StreamReader( stream ) )
    23.                             {
    24.                                 resultText = streamReader.ReadToEnd();  // Crashed at this line.                          
    25.                             }
    26.                             code = ((HttpWebResponse)ex.Response).StatusCode;                                                    
    27.                             ex.Response.Close();
    28.                         }                    
    29.                         return;
    30.                     }
    31.                  
    32.                 }
    33. }
    It's almost same issue described at below links

    https://stackoverflow.com/questions...et-response-from-web-request-when-400-returne

    https://github.com/mono/mono/issues/10645

    Any help will be appreciated!

    Thanks!
     
    Last edited: Jul 16, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
  3. gguolinegames

    gguolinegames

    Joined:
    Mar 7, 2019
    Posts:
    8
    I tried postman and curl and all these worked well.

    Anyway Thanks for response!