Below is my request i get a 500 internal server error at getresponse
string requestData = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><h:HeaderItem xmlns:h=\"http://tempuri.org/\">a header item</h:HeaderItem><ActivityId CorrelationId=\"090c553b-bfcc-4e4f-94cd-1b4333fe82a9\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">377a454b-b543-4c6f-b4ac-3981029b60e6</ActivityId></s:Header><s:Body><string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">a body item</string></s:Body></s:Envelope>";
byte[] requestDataBytes = Encoding.UTF8.GetBytes(requestData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/WebService/");
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
request.ContentLength = requestDataBytes.Length;
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
streamW开发者_开发知识库riter.Write(requestData);
streamWriter.Flush();
streamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
string responseBody = streamReader.ReadToEnd();
I may have an answer for you.
Move the line where you set your headers to the point before you set the content type, and retry your code, as follows:
request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
request.ContentType = "text/xml; charset=utf-8";
I make this suggestion based on the documentation for the ContentType property for the WebRequest object from MS:
MS docs on WebRequest
The value for this property is stored in WebHeaderCollection . If WebHeaderCollection is set, the property value is lost.
Now, I realize we're not expressly setting the WebHeaderCollection, but you are setting a header in that collection, and it made me suspect at least the possibility of this being a problem - rendering your existing ContentType blank, and being interpreted as some default on the inbound side of the web service.
Maybe a long shot, but it might be worth a try.
You should enable WCF trace logging on your server and see if it indicates an error. Typically errors that occur during deserialization or in the WCF layers before hitting your app code will write to this log.
For more info on setting up WCF trace logging, see here or here.
精彩评论