Is HttpWebRequest the proper way to make soap request?

grayblizzard

New Member
I've used a a few service references in vb.net/vs 2010, but this was the first one that both required a certificate and a custom soap header. I was given a WSDL and request xml which I tested successfully in soapUI 3.5. After creating a service reference in vb.net I could not figure out how to add a custom soap header. I saw many examples that implement \[code\]IClientMessageInspector\[/code\], but could not get any of them to work for one reason or another. Most had to do with my lack of understanding of the Implements keyword, but there was also an issue with endpoint behaviors. I already use an endpoint behavior for the certificate and could not get two endpoint behaviors to work on a single endpoint.I switched to HttpWebRequest, but can't get it to work either. The code runs, but I get a similar error from the server. The error is \[code\]"Remote server returned an error:(500) Internal Server Error".\[/code\] I used Fiddler to see the actual request and response, and the response is from their data power server is, \[code\]"TID:247456978.Catastrophic Error. Please call IAS Datapower Support [ 'dp:reject' executed - Unable to find metadata for 'long_url' ]".\[/code\] The developer's response is that their system is running fine, so the problem is mine. I agree because I can get a valid response using soapUI.So the question is, what am I doing wrong. Does the code below even look right. Am I on the right track or should I toss this attempt and look for another solution to making a soap request without using a service reference. \[code\] Public Shared Sub TryIt() Try Dim sSoapEnv As String = "<soap:Envelope> {Rest of soap envelope here}" sSoapEnv &= "</soap:Envelope>" 'MsgBox(sSoapEnv) ' Create a request using a URL that can receive a post. Dim request As HttpWebRequest = HttpWebRequest.Create("https://yada.yada.yada.com:443/restofurl") Dim cert As New X509Certificate2 cert.Import("T:\fullpathtocert\fw.hdnipa.com.p12", "cert_password", X509KeyStorageFlags.PersistKeySet) Dim cred As New NetworkCredential("gdebacke.fmcdmn.local", "domain_password") request.Credentials = cred request.ClientCertificates.Add(cert) ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim postData As String = sSoapEnv Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As HttpWebResponse = request.GetResponse() ' Display the status. Console.WriteLine(response.StatusDescription) ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. Console.WriteLine(responseFromServer) ' Clean up the streams. reader.Close() dataStream.Close() response.Close() Catch ex As Exception MsgBox(ex.Message) End TryEnd Sub\[/code\]
 
Back
Top