locked
HttpWebRequest Get.Response Error : The remote server returned an error: (500) Internal Server Error RRS feed

  • Question

  • User-770252936 posted

    Hello

    I'm trying to establish a connection, but var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse(); I get the following error in the line of code: The remote server returned an error: (500) Internal Server Error

    I was unable to resolve this error

    My codes are :

    try
    {
    	ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    
    	string base64 = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "kortmazsefa", "xxxxxx")));
    
    	var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://dev.gittigidiyor.com:8080/listingapi/ws/CategoryService?wsdl");
    	httpWebRequest.Headers.Add("Authorization", "Basic "+ base64);
    	httpWebRequest.ContentType = "text/xml";
    	httpWebRequest.Method = "POST";
    
    	XmlDocument requestXml = new XmlDocument();
    	requestXml.LoadXml("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cat=\"http://category.anonymous.ws.listingapi.gg.com\">" +
                                   "<soapenv:Header/>" +
                                   "<soapenv:Body>" +
                                   "<cat:getCategories>" +
                                   "<startOffSet>0</startOffSet>" +
                                   "<rowCount>4</rowCount>" +
                                   "<withSpecs>true</withSpecs>" +
                                   "<lang>tr</lang>" +
                                   "</cat:getCategories>" +
                                   "</soapenv:Body>" +
                                   "</soapenv:Envelope>");
    
    	using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    	{
    		requestXml.Save(streamWriter);
    	}
    
    	var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    	using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    	{
    
    	}
    }
    catch (Exception e)
    {
    	Response.Write(e);
    }

    Thursday, August 8, 2019 6:44 AM

Answers

  • User-719153870 posted

    Hi kortmazsefa,

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

    This is most likely caused by error in your httpWebRequest.

    Please try :

    HttpWebResponse httpResponse;
    try
     {
         httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
     }
    catch (WebException ex)
     {
         httpResponse = (HttpWebResponse)ex.Response;
     }

    insead of :

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    In this case, when an exception occurs, not only does StatusCode mark the error code of HTTP in WebException, but its Response attribute also contains the WebResponse sent by the server to indicate the actual HTTP error encountered.

    You can also refer to here for more details.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 8:38 AM