none
The remote server returned an error: (500) Internal Server Error RRS feed

  • Question

  • Hello,

    I am using an URL trying to retreive information from an API. When I paste this URL in the Chrome browser, I get the correct information: (Note I have not included user/password so it wont work for you)

    https://www.URL.com/api/rest/security/session?username=theusername&password=thepassword

    However, when I try to use the webrequest code below. I get the error:

    "The remote server returned an error: (500) Internal Server Error"

    The response looks like this in this in the browser which is correct.

    {"session-token":"12345_123156468aadf564fa6sfa8fs6af87","user-id":12345}

    I wonder if I do anything wrong in the below code? (I know POST is correct to use here)

     private void button1_Click(object sender, EventArgs e)
            {
                
                String responseBody = "";
                String URL = "https://www.URL.com/api/rest/security/session?username=theusername&password=thepassword";
                request2(URL, out responseBody);
    
    
                MessageBox.Show(responseBody);
            }
            public void request2(String requestString, out String responseBody)
            {
                responseBody = "";
                try
                {
                    //Make the request
                    var request = (HttpWebRequest)WebRequest.Create(requestString);
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
                    request.Timeout = 15000;
                    request.Method = "POST";
                    request.Accept = "application/json";
                    request.ContentType = "application/json; charset=utf-8";
    
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        var stream = response.GetResponseStream();
                        responseBody = "";
                        using (var reader = new StreamReader(stream))
                        {
                            responseBody = reader.ReadToEnd();
                            reader.Close();
                        }
                        response.Close();
                        responseBody = responseBody.ToLower();
                    }
                }
                catch (WebException ex) { MessageBox.Show(ex.ToString()); }
            }







    • Edited by Silvers2 Monday, February 29, 2016 4:41 PM
    Monday, February 29, 2016 4:39 PM

Answers

  • I now got a successfull response. 
    I beleive the below lines was the key to add:

                    string credentialsDetail = "{'username': '" + "username" + "','password': '" + "password" + "'}";
                    JObject jsonData = JObject.Parse(credentialsDetail);
                    request.ContentLength = jsonData.ToString().Length;


    • Marked as answer by Silvers2 Monday, February 29, 2016 10:16 PM
    Monday, February 29, 2016 10:16 PM

All replies

  • you crashed the server code. Ask the server developer to fix this. http status code should be more descriptive. 


    Visual C++ MVP

    Monday, February 29, 2016 5:01 PM
  • Thanks for answer,

    >> http status code should be more descriptive. 

    I am not sure how you mean there. Is there any kind of http status I get get with the error to show any clue to the problem?

    I have contacted the server developer and for them the exact code works which is strange.

    Monday, February 29, 2016 5:08 PM
  • The server developer can turn on logging or check the event log to see where the code crashed. There isn't much you can do besides writing request sticky to what the server developer dictates. 500 server side error could be anything happening on the server, e.g. out of memory, out of disk space, database connection timeout, sql injection bugs, etc. 



    Visual C++ MVP

    Monday, February 29, 2016 5:44 PM
  • I have talked to them now 2 times and they use something called "Postman" and say that that the request is successfull. I do wonder if something is missing in my code since this is a big company and it shouldn't be any wrong on their server.

    When I paste the URL in the chrome browser:

    https://www.URL.com/api/rest/security/session?username=theusername&password=thepassword

    Then I receive a successfull response in the browser which leads me again to that something is wrong or missing in the HttpWebRequest code?




    • Edited by Silvers2 Monday, February 29, 2016 6:32 PM
    Monday, February 29, 2016 6:23 PM
  • run fiddler and compare the requests you made and chrome made. 


    Visual C++ MVP

    Monday, February 29, 2016 6:33 PM
  • That was a good idéa. I installed Fiddler, I made a request in chrome and I looked under the tab "Headers" and could see some additional properties. I added those in the code below under:

    "Additional lines"

    I did add those 4 additional parameters in the webrequest code (I wonder if I have added them correctly?)

    However, this still gives the same 500 server error. I must be missing something more. Could something with the code be wrong or is there anything else I can look up in Fiddler?

            private void button2_Click(object sender, EventArgs e)
            {
                
                String responseBody = "";
                String URL = "http://www.URL.com/bpapi/rest/security/session?username=theusername&password=thepassword";
                request2(URL, out responseBody);
    
    
                MessageBox.Show(responseBody);
            }
            public void request2(String requestString, out String responseBody)
            {
                responseBody = "";
                try
                {
                    //Make the request
                    var request = (HttpWebRequest)WebRequest.Create(requestString);
                    request.Timeout = 15000;
                    request.Method = "POST";
                    request.ContentType = "application/json; charset=utf-8";
    
                    //Additional lines
                    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
                    request.Accept = "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
                    request.Headers.Add("Accept-Language", "sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4");
                    request.KeepAlive = true;
    
                  
    
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        var stream = response.GetResponseStream();
                        responseBody = "";
                        using (var reader = new StreamReader(stream))
                        {
                            responseBody = reader.ReadToEnd();
                            reader.Close();
                        }
                        response.Close();
                        responseBody = responseBody.ToLower();
                    }
                }
                catch (WebException ex) { MessageBox.Show(ex.ToString()); }
            }



    • Edited by Silvers2 Monday, February 29, 2016 7:54 PM
    Monday, February 29, 2016 7:52 PM
  • Since you got no help from the server developer, compare and compare your requests and chrome's. byte by byte. 


    Visual C++ MVP

    Monday, February 29, 2016 8:26 PM
  • I am not sure what to look for in Fiddler more than the below things that I have found?

    Could there be any more specific parameter I can compare?

     request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
                    request.Accept = "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
                    request.Headers.Add("Accept-Language", "sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4");
                    request.KeepAlive = true;

    Monday, February 29, 2016 8:35 PM
  • I now got a successfull response. 
    I beleive the below lines was the key to add:

                    string credentialsDetail = "{'username': '" + "username" + "','password': '" + "password" + "'}";
                    JObject jsonData = JObject.Parse(credentialsDetail);
                    request.ContentLength = jsonData.ToString().Length;


    • Marked as answer by Silvers2 Monday, February 29, 2016 10:16 PM
    Monday, February 29, 2016 10:16 PM