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

HTTP/1.1 500 Internal Server Error While calling a web service by wwwForm

Discussion in 'Editor & General Support' started by Sumit Kumar, Dec 6, 2012.

  1. Sumit Kumar

    Sumit Kumar

    Joined:
    Oct 19, 2012
    Posts:
    1
    HI,

    I am creating the web build which will run on the unity web player . I am getting the error which states that HTTP/1.1 500 Internal Server Error while calling the web service hosted on our data server.. But when i called the web service hosted on my IIS Server , it works fine. and the web player run the game as well on the browser. I am stuck with this problem . Can someone help me to solve this problem. I am just exhausted by this error. Thanks in advance.
     
  2. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    500 errors are most typically server misconfiguration errors of some kind. Unfortunately, they are a massive b**ch to track down as there's no definitive starting point. Some common causes for 500 errors are:

    Capitalization of URLs (only affects Apache/Unix servers). Apache sees "some-Name.html" and "some-name.html" as different pages. If your data server is Apache and the scripts run fine on your IIS server, there's a good chance this is your problem.

    Htaccess errors - if your htaccess file has a mistake in it that causes the server to loop endlessly, then you'll get a 500. Likewise, if the htaccess is very complex, you might reach a point where the server is not sure which page it's supposed to show, and returns a 500.

    Database timeouts - if your data retrieval from a SQL DB is complex and large, the connection can go to sleep (shut down silently), but on some configurations this causes the server to get confused while running your scripts because it thinks the DB is just busy. If the 500 error comes back quickly, this is not your problem - this error usually comes up after the default DB timeout of 30 seconds.

    Bad headers - depending on what you are returning from your scripts, if the headers are malformed or in some cases completely missing (rare) then you might get a 500 error as a default error instead of anything more useful. This is quite a rare occurrence, and chances are if you know enough web dev to be modifying headers to this degree, then you've probably already checked this.

    As you say it runs fine on one server but not another, I'd start going through the configurations of both servers to ensure there are no obvious differences. It will most likely be something simple, but not necessarily obvious.
     
  3. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,204
    If this is a .Net webservice, then it's usually because of a bug in the webservice - unable to find and/or deserialize the objects (not passed correctly, XML is wrong, etc). You should put a breakpoint in the webservice where it is being called, and step into it to see what is happening.
    If it's not a .Net webservice, then I have nothing for you.
     
  4. wechat_os_Qy00Ucf9AazrQ3c29B_s7Nv7o

    wechat_os_Qy00Ucf9AazrQ3c29B_s7Nv7o

    Joined:
    Mar 27, 2020
    Posts:
    3
  5. junedmmn

    junedmmn

    Joined:
    Dec 19, 2016
    Posts:
    15
    This thread is old, but I would like to answer. Unity added
    UnityWebRequest
    to deal with webservices, webpages and APIs.
    Use
    UnityWebRequest
    and set its
    UploadHandler
    to
    UploadHandlerRaw

    As you have not given any code I would add my sample code
    Code (CSharp):
    1.     UnityWebRequest req = UnityWebRequest.Post("http://localhost:58755/User/AddNewUser", "POST");
    2.     req.SetRequestHeader("Content-Type", "application/json");
    3.     req.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(user))) as UploadHandler;
    4.     req.certificateHandler = new BypassCertificate();
    5.  
    6.     yield return req.SendWebRequest();
    7.  
    8.     if (req.isNetworkError || req.isHttpError || req.isError)
    9.         print("Error: " + req.error);
    10.  
    11.     print(req.downloadHandler.text);
    12.  
    In case someone want to know about
    BypassCertificate
    . It is used to bypass certificate validation. I was using ASP.NET Core Webservice. it was causing problems. Hence, this bypasses certificate validation. Place this class anywhere in your code out of your current class.

    Code (CSharp):
    1. public class BypassCertificate : CertificateHandler
    2. {
    3.     protected override bool ValidateCertificate(byte[] certificateData)
    4.     {
    5.         return true;
    6.     }
    7. }
    Refer to this answer by me for full code: https://stackoverflow.com/q/65442948/8304176
     
    Last edited: Dec 24, 2020
  6. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    In which case, we have to use UploadHandlerRaw?
    Because I don't have "user" variable to pass, I have 3 of int variables to pass as web request and receive response based on this web service call.
    Occasionally I was getting this error, sometimes coming and sometime working properly so it become difficult for me to figure out.
    Any suggestion will be welcome :)
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You will want to test your web service via curl directly from a command line to ensure that your web service is working properly, start with the basics. You can also use a tool like PostMan. Then use a tool like Fiddler or Charles Proxy to compare the request coming out of your Unity app to the working request from curl/PostMan https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity
     
    siddharth3322 likes this.
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Is it your server?
    The error 500 means an error happened on server side (commonly an exception was thrown and not handled). Such failures are typically a combination of problems with both your request and server-side code, hence having access to the server helps, you can investigate what blew up there.
    Fix is also on both ends: a well written server should never give 5xx error, it should give 4xx response, preferably detailing what it didn't like in request.
     
    siddharth3322 and JeffDUnity3D like this.
  9. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    @JeffDUnity3D and @Aurimas-Cernius Thank you to both of you for your suggestions :)
    Now let me reply for your questions and share more details from my side.

    From last few days I don't have this issue but today again started coming:
    500 internal server error.png

    This is the error that started showing again and with my surprise, I was getting this error within the same web service through my project includes multiple web services and communication with the web server.

    But only this web service showing 500 internal web server error.
    If I am executing web service on PostMan then its working properly, I didn't notice this problem in PostMan.
    Only Unity editor giving this error.

    Currently I am using Unity 2021.1.13f1 for my project.
    This is our custom web server that we were using to hold all server things and communicating through different web services.
    After reading this details, please share your thoughts for this problem.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    You need to study what is going out and figure out how it differs.

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    As Kurt mentioned, use Charles Proxy to see the differences between the call that works and the one that doesn't
     
    siddharth3322 likes this.
  12. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Okay sir let me do this :)