Reputation: 2376
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:
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 :)
Upvotes: 2
Views: 2513
Reputation: 17648
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();
}
}
Upvotes: 5