2

I am getting an error on HttpWebRequest.GetRequest() when trying to send xml request on one web service for response. This same request working from Postman. Please guide in right direction to solve this. Below is a XML Body text and code.

    string xmlMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
  "<soap:Body>" 
"<GetDetail xmlns=\"http://tempuri.org/\">" 
  "<strNo>22101</strNo> <baCode></baCode> <authkey>xxxx</authkey><source>xxxx</source>" 
"</GetDetail>" "</soap:Body>" "</soap:Envelope>";


        byte[] requestInFormOfBytes = System.Text.Encoding.ASCII.GetBytes(xmlMessage);
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://xxxxxxx.xx/xxxx/GetDetail");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/xml";
        webRequest.Headers.Add("SOAPAction", "http://tempuri.org/GetDetail");
        webRequest.Headers.Add("Client-Id", "XXXXXXXXXXXXX");
        webRequest.Headers.Add("Client-Secret", "XXXXXXXXXXXXXXXXX");
        webRequest.Accept = "application/xml";
        webRequest.ContentLength = requestInFormOfBytes.Length;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Error give on this line.

Stream requestStream = webRequest.GetRequestStream();

 requestStream.Write(requestInFormOfBytes, 0, requestInFormOfBytes.Length);
        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
        string receivedResponse = respStream.ReadToEnd();
        Console.WriteLine(receivedResponse);
        respStream.Close();
        response.Close();

Thanks for all to give right direction, its resolved by (1) Test with FrameWork 4.6 (2) Use SecurityProtocoltype.Tls12 (refer - https://stackoverflow.com/a/32789483/5694613).

bd coder
  • 51
  • 1
  • 7
  • That requestStream.Close looks a bit iffy - it will dispose the stream, instead put `Stream requestStream = ` inside a `using` block (The StreamReader & response stream should also be within using blocks) – Alex K. May 24 '19 at 13:28
  • `ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;` Do you really have to do that? Maybe just let the default negotiation happen. Also please provide the full exception stracktrace. – thehennyy May 24 '19 at 13:36
  • `ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;`. Sure about that? It's deprecated. Anyway, you need to specify it **before** you initialize the connection. Also, a many services also require that you validate the server certificate, returning the validation result using a: [ServicePointManager.ServerCertificateValidationCallback](https://learn.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.servercertificatevalidationcallback). Verify the SSL protocol that is actually required. – Jimi May 24 '19 at 13:40
  • Thanks for reply. I have tried without ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; and also with ".Tls, .Tls11, .Tls12" but same error. I am wondering same request work in Postman, if use Code from Postman of C#(RestSharp) this same error occurred. – bd coder May 24 '19 at 14:14
  • Please post the entire stack trace. If you can't figure it out from there, I recommend installing Fiddler and inspect what is happening at a lower level. Probably something going wrong in the handshake. – Bart van der Drift May 24 '19 at 14:19
  • Hi Bart van der Drift in Exception StackTrace shows - at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at WindowsApplicationC.Form1 – bd coder May 24 '19 at 14:29
  • Error Resolved with help of below link. https://stackoverflow.com/a/32789483/5694613 – bd coder May 27 '19 at 05:42

0 Answers0