HttpWebRequest is not POSTing data RRS feed

  • Question

  • User-736832430 posted
    Hi Experts,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><o:p> </o:p>I am stuck into very annoying problem; what I am trying to do is very simple but just can not get this working.<o:p></o:p>Basically; I am trying to POST some data over HTTPS to our 3<SUP>rd</SUP> party client. I am passing all parameters but their response comes as Missing Mandatory parameters and I believe my data is not getting POST thrie servers.<o:p></o:p><o:p> </o:p>Please help.<o:p></o:p><o:p> </o:p>
    Below is my code.<o:p></o:p>
    Dim sUri As String = "https://testurl.jsp"<o:p></o:p>    <o:p></o:p>    Dim myUri As New System.Uri(sUri)<o:p></o:p><o:p> </o:p><o:p> </o:p>    Dim HttpStream As Stream<o:p></o:p><o:p> </o:p><o:p> </o:p>        Try<o:p></o:p>    Dim xmlDoc As New XmlDocument()<o:p></o:p>            xmlDoc.Load("C:\Sample.xml")<o:p></o:p><o:p> </o:p><o:p> </o:p><o:p> </o:p>    Dim bdata As Byte() = Encoding.UTF8.GetBytes(xmlDoc.InnerXml.ToString())<o:p></o:p><o:p> </o:p>    Dim myHttpWebRequest As HttpWebRequest = DirectCast(WebRequest.Create(myUri), HttpWebRequest)<o:p></o:p><o:p> </o:p>            myHttpWebRequest.ContentType = "text/xml"<o:p></o:p>    ' "application/x-www-form-urlencoded";// <o:p></o:p>            myHttpWebRequest.Method = "POST"<o:p></o:p>            myHttpWebRequest.Timeout = 100000<o:p></o:p>            myHttpWebRequest.AllowWriteStreamBuffering = True<o:p></o:p>            myHttpWebRequest.ContentLength = bdata.Length<o:p></o:p><o:p> </o:p><o:p> </o:p>    'a reference to the HttpWebRequest's request stream. <o:p></o:p>    Dim myWriter As StreamWriter = Nothing<o:p></o:p>            myWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())<o:p></o:p>            myWriter.Write(xmlDoc.InnerXml.ToString())<o:p></o:p>            myWriter.Close()<o:p></o:p>    'HttpStream = DirectCast(myHttpWebRequest.GetRequestStream(), Stream)<o:p></o:p>    'HttpStream.Write(bdata, 0, bdata.Length)<o:p></o:p>    'HttpStream.Close()<o:p></o:p><o:p> </o:p><o:p> </o:p>    ' Send the request and get a response. <o:p></o:p>    Dim myHTTPResponse As HttpWebResponse = DirectCast(myHttpWebRequest.GetResponse(), HttpWebResponse)<o:p></o:p><o:p> </o:p>    <o:p></o:p>    Dim iStatCode As Integer = CInt(myHTTPResponse.StatusCode)<o:p></o:p>    Dim sStatus As String = iStatCode.ToString()<o:p></o:p>            Console.WriteLine("Status Code: {0}", sStatus)<o:p></o:p>    <o:p></o:p>    Dim responseXML As New XmlDocument()<o:p></o:p>            responseXML.Load(myHTTPResponse.GetResponseStream())<o:p></o:p><o:p> </o:p>            TextBox1.Text = responseXML.InnerXml<o:p></o:p><o:p> </o:p>   <o:p></o:p>            myHttpWebRequest = Nothing<o:p></o:p>            myHTTPResponse = Nothing<o:p></o:p>    ' strm = Nothing<o:p></o:p><o:p> </o:p><o:p> </o:p>    'sr = Nothing<o:p></o:p>        Catch e As WebException<o:p></o:p>            Console.WriteLine("WebException raised!")<o:p></o:p>            Console.WriteLine(vbLf & "{0}", e.Message)<o:p></o:p>            Console.WriteLine(vbLf & "{0}", e.Status)<o:p></o:p>        Catch e As Exception<o:p></o:p>            Console.WriteLine("Exception raised!")<o:p></o:p>            Console.WriteLine("Source :{0} ", e.Source)<o:p></o:p>            Console.WriteLine("Message :{0} ", e.Message)<o:p></o:p><o:p> </o:p>        End Try<o:p></o:p>    End Sub<o:p></o:p>



    Friday, June 12, 2009 10:56 AM

All replies

  • User-581386078 posted

     Have you tried running something like Fiddler to spy on the request?

    Friday, June 12, 2009 4:08 PM
  • User-736832430 posted
    Hi there,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><o:p> </o:p>I have some interesting findings from fiddler. Fiddler gives me my HTTPs response code as 0 while my code does return response code as 200 with response object. <o:p></o:p>When I turned on Decrypt HTTPS messages on Fiddler it gave me response code 200 but my client never got response object like Missing mandatory parameters as I used to get before; while I received SSl trust certification error.<o:p></o:p><o:p> </o:p>

    Is my code OK? Why I am struggling to get this working?



    Rahul<o:p> </o:p><o:p> </o:p>


    Monday, June 15, 2009 9:40 AM