1

After doing some investigation and reading several posts about it and in particular this one :

I came up with the following code:

RequestingXMLData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                    <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:iaso-com:cloud:mansvc:15.12.0"">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <urn:Authenticate>
                                <request>
                                    <partnerName>XXX</partnerName>
                                    <userName>XXX</userName>
                                    <password>XXX</password>
                                </request>
                            </urn:Authenticate>
                        </soapenv:Body>
                    </soapenv:Envelope>";

//Retrieves XML Data from API link / service, optional to spit XML to a given file location
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ApiCallPath);
request.Method = "POST";
request.ContentType = "text/xml;charset=\"utf-8\"";
request.ContentLength = RequestingXMLData.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
{
    requestWriter.Write(RequestingXMLData);
}

try
{
    WebResponse webResponse = request.GetResponse();
    using (Stream webStream = webResponse.GetResponseStream())
    {
        if (webStream != null)
        {
            using (StreamReader responseReader = new StreamReader(webStream))
            {
                response = responseReader.ReadToEnd();
            }
        }
    }
}
catch (WebException e)
{
    var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();

}

return response;

I keep getting the 500 error at the WebResponse webResponse = request.GetResponse(); part.

As suggested in several posts I downloaded Fiddler and the response I get from it looks fine (Not experienced with it though so I could be off):

Request Count:   1
Bytes Sent:      85     (headers:85; body:0)
Bytes Received:  107        (headers:107; body:0)
Tunnel Sent:     1.740
Tunnel Received: 4.900

ACTUAL PERFORMANCE
--------------
ClientConnected:    11:03:28.895
ClientBeginRequest: 11:03:28.900
GotRequestHeaders:  11:03:28.900
ClientDoneRequest:  11:03:28.901
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect: 12ms
HTTPS Handshake:    0ms
ServerConnected:    11:03:28.916
FiddlerBeginRequest:    11:03:28.916
ServerGotRequest:   11:03:28.916
ServerBeginResponse:    00:00:00.000
GotResponseHeaders: 00:00:00.000
ServerDoneResponse: 00:00:00.000
ClientBeginResponse:    11:03:28.917
ClientDoneResponse: 11:03:28.917

    Overall Elapsed:    0:00:00.017

RESPONSE BYTES (by Content-Type)
--------------
~headers~: 107

Next thing I did was trying to retrieve the WebException and this is what it is returning: enter image description here

I also tested it in SOAP UI and there it seems to work - after fixing a Java error (so the authenticate data is correct as well).

Some additional info:

Keep in mind that I never done something before with SOAP, so my apologies if the problem seems obvious :)

Community
  • 1
  • 1
Nicolas
  • 2,277
  • 5
  • 36
  • 82
  • 1
    `new StreamWriter(webStream, System.Text.Encoding.ASCII))` Try `System.Text.Encoding.UTF8` – Stefan Jan 17 '17 at 10:22
  • After changing it I am getting an exception at the changed part `using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))` Exception: `The request was aborted: The request was canceled.` – Nicolas Jan 17 '17 at 10:31
  • Hmm, that's odd. Let me check again – Stefan Jan 17 '17 at 10:33
  • Perhaps issues with the calling SOAP XML? Will test the answer given in this post: http://stackoverflow.com/questions/14499006/httpwebrequest-was-cancelled BTW stupid that I missed that ASCII encoding ;) – Nicolas Jan 17 '17 at 10:35
  • Hmm, might be a keep-alive issue as well. Is the exception raised immediately or does it take some time (some seconds)? http://stackoverflow.com/questions/2459241/httpwebrequest-the-request-was-aborted-the-request-was-canceled#2950533 – Stefan Jan 17 '17 at 10:38
  • It is raised immediately. Also says that the Webexception was unhandled by the way but after adding a Try Catch to it it returns the same aborted error. – Nicolas Jan 17 '17 at 10:39
  • Quite strange. The error happens when it wants to leave the using statement: `using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8)) { requestWriter.Write(RequestingXMLData); }` When I debug over the closing } it pops the error. Also added the suggestion in your submitted post: http://stackoverflow.com/questions/2459241/httpwebrequest-the-request-was-aborted-the-request-was-canceled#2950533 – Nicolas Jan 17 '17 at 10:47
  • Hi, just finished lunch XD.. Ah, yes, just to test it: omit the `using` statement in `using (Stream webStream = request.GetRequestStream())` It seems that the stream is closed premature. – Stefan Jan 17 '17 at 11:04
  • You could try the pattern mentioned here: http://stackoverflow.com/questions/1799005/soap-object-over-http-post-in-c-sharp-net#1799091 – Stefan Jan 17 '17 at 11:05
  • Even better: check this example on msdn: https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream(v=vs.110).aspx – Stefan Jan 17 '17 at 11:10
  • Thanks just (fast) tested MSDN example and that seems to work. It seems to return all the protocol versions though listed here : https://cloud.iaso.com/manserv. I will check your updated answer postafter I finished my own lunch ;) – Nicolas Jan 17 '17 at 11:24

1 Answers1

4

First you say:

 request.ContentType = "text/xml;charset=\"utf-8\"";

Translation: Hey, I've got a message, it's in the form of XML and UTF8 byte encoding.

Then you'll sneak up and do:

using (StreamWriter requestWriter = 
            new StreamWriter(webStream, System.Text.Encoding.ASCII))

Translation: Passing some dark ancient mysterious (clay)tablet, containing some ASCII.

So, stick with your promise and use:

using (StreamWriter requestWriter = 
            new StreamWriter(webStream, System.Text.Encoding.UTF8))

By the way, UTF8 is the way to go when you're doing XML based things like soap, or html even.

As for the aborted exception, it seems your connection is prematually disposed. You might want to try to put your try statement inside the using block:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("")

request.Method = "POST";
request.ContentType = "text/xml;charset=\"utf-8\"";
request.ContentLength = RequestingXMLData.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))
{
   requestWriter.Write(RequestingXMLData);

   try
   {
       WebResponse webResponse = request.GetResponse();
       using (Stream webStream = webResponse.GetResponseStream())
       {
            if (webStream != null)
            {
                 using (StreamReader responseReader = new StreamReader(webStream))
                 {
                     response = responseReader.ReadToEnd();
                 }
             }
       }
   }
   catch (WebException e)
   {
        var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();

   }
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • When modifying my code to your last updated code part it returns several other errors. The webstream of `using (Stream webStream = webResponse.GetResponseStream())` cannot be declared in this scope because because it would give a different meaning to 'webstream'. When changing it to 'webstream2' it gives the error: `You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.` – Nicolas Jan 17 '17 at 12:42