Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am sending a request to external API from my application using HttpWebRequest and HttpWebResponse.

Whenever there is an error in XML request due to some wrong values passed in XML nodes, then there is a 400 Bad request error response and the error message is "There is an error in XML document (51, 14)". The problem is if there is an error in request XML, the response XML should display the error message with correct line number. but I am receiving an error message with incorrect line number and it is always "There is an error in XML document (1, 1254)". There is actually no error at line 1. Due to this issue, I have no chance to point to the error when troubleshooting.

Can you help how do I get Response with the correct line number in XML.

Thanks

What I have tried:

Below is my existing code in vb to send the request to API.

Dim Wreq As HttpWebRequest
Dim MyURI As String = String.Empty
Dim bytes() As Byte
Try
MyURI = p_strURL
Wreq = HttpWebRequest.Create(MyURI)
Wreq.Method = "POST"
bytes = System.Text.Encoding.UTF8.GetBytes(pi_strRequestXML)
Wreq.ContentLength = bytes.Length 'pi_strRequestXML.Length
Wreq.ContentType = "application/x-www-form-urlencoded"
Wreq.KeepAlive = False
Wreq.Headers.Add("Authorization", "bearer" + " " + strAccessToken)
Using OutputStream As StreamWriter = New StreamWriter(Wreq.GetRequestStream())
OutputStream.Write(pi_strRequestXML)
End Using

Using Wres As HttpWebResponse = Wreq.GetResponse()
Using loResponseStream As StreamReader = New StreamReader(Wres.GetResponseStream())
oResponse = loResponseStream.ReadToEnd()
End Using
End Using
Return oResponse
Catch e As WebException
Throw
Catch objSysEx As Exception
Throw
Finally

End Try
Posted
Updated 28-Apr-18 3:01am
v2
Comments
Peter_in_2780 27-Apr-18 4:32am    
It looks like line endings (cr? crlf? lf?) are getting stripped out of the XML somewhere along the way. Check exactly what you are sending.
Richard Deeming 27-Apr-18 10:06am    
Catch e As WebException
    Throw e
Catch objSysEx As Exception
    Throw objSysEx


Don't do that! You've just destroyed the stack trace of your exceptions, which will make it almost impossible to work out where they're thrown from.

If you really need to re-throw an exception, use Throw, not Throw ex:
Catch e As WebException
    Throw
Catch objSysEx As Exception
    Throw


But in this case, since you're not doing anything with the exceptions, you should simply remove the Try..Catch..Finally block, and let the exceptions propagate to the calling method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900