简体   繁体   English

400 错误请求:XML 文档中有错误 (1, 1254) 消息显示不正确的行号

[英]400 bad request : there is an error in XML document (1, 1254) message showing incorrect line number

I am sending a request to external API from my application using HttpWebRequest and HttpWebResponse.我正在使用 HttpWebRequest 和 HttpWebResponse 从我的应用程序向外部 API 发送请求。

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)".每当由于 XML 节点中传递的某些错误值而导致 XML 请求出现错误时,就会出现 400 Bad request 错误响应,错误消息为“XML 文档中存在错误 (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.问题是如果请求 XML 中有错误,响应 XML 应该显示带有正确行号的错误消息。 but I am receiving an error message with incorrect line number and it is always "There is an error in XML document (1, 1254)".但我收到一条错误消息,行号不正确,并且总是“XML 文档中存在错误 (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.第1行实际上没有错误。由于这个问题,我在故障排除时没有机会指出错误。

Can you help how do I get Response with the correct line number in XML.你能帮助我如何在 XML 中使用正确的行号获得响应。

Below is my existing code in vb to send the request to API.下面是我在 vb 中将请求发送到 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

Thanks谢谢

Finally, I could find and solve this issue.最后,我可以找到并解决这个问题。 The problem was in request XML that I was trying to send.问题出在我试图发送的请求 XML 中。 Request XML is not correctly formatted and so it was showing inappropriate line numbers if there is an error in XML.请求 XML 的格式不正确,因此如果 XML 中有错误,它会显示不适当的行号。 The solution for this was to format request XML with proper indentation.对此的解决方案是使用适当的缩进格式化请求 XML。 Below is the code I used to make well-formatted XML.下面是我用来制作格式良好的 XML 的代码。

Using sw As New StringWriterWithEncoding(Encoding.UTF8)
    Using tw As New XmlTextWriter(sw)
     'tw.Settings.Encoding = Encoding.UTF8
      tw.Formatting = Xml.Formatting.Indented
      tw.Indentation = 4
      Dim docu As New XmlDocument
      docu.LoadXml(strXML)
      docu.Save(tw)
      strProfileXML = Convert.ToString(sw)
   End Using
End Using

in above code, StringWriterWithEncoding is the function that is used to create formatted XML with UTF-8 encoding.在上面的代码中,StringWriterWithEncoding 是用于创建带有 UTF-8 编码的格式化 XML 的函数。 if not used this function then XML will be created with UTF-16如果不使用此功能,则将使用 UTF-16 创建 XML

Below is the function to use to set encoding for XML下面是用于设置 XML 编码的函数

Private NotInheritable Class StringWriterWithEncoding
        Inherits StringWriter
        Private ReadOnly m_encoding As Encoding
        Public Sub New(encoding As Encoding)
            m_encoding = encoding
        End Sub
        Public Overloads Overrides ReadOnly Property Encoding() As Encoding
            Get
                Return m_encoding
            End Get
        End Property
    End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XML POST上的400错误请求 - 400 Bad Request on XML POST 错误请求:无法解析 XML 400 错误 Curl PHP - Bad Request: XML cannot be parsed 400 error Curl PHP 结合使用jQuery Ajax和XML时出现错误请求(400)错误 - Bad Request (400) error when using jQuery Ajax with XML 400错误的休息申请/ XML请求 - 400 bad request for rest application/xml 在 node.js 中发送 XML 数据的 POST 请求:错误 400,“客户端发送的请求在语法上不正确” - Send POST request with XML data in node.js: Error 400, "The request sent by the client was syntactically incorrect" 发布XML数据时WCF Restful服务收到错误400(错误请求) - WCF Restful services getting error 400 (bad request) when post xml data PHP NuSoap中的XML请求-“ HTTP / 1.1 400错误请求” - XML request in PHP NuSoap - “HTTP/1.1 400 Bad Request” 带有POST和xml的Cisco ISE REST HttpUrlConnection 400错误请求 - Cisco ISE REST HttpUrlConnection 400 Bad Request with POST and xml in body 400错误请求将xml有效负载发送到WCF REST服务 - 400 bad request sending xml payload to WCF REST service Windows 7上的IIS:请求XML文件时出现400错误请求 - IIS on Windows 7: 400 bad request when requesting XML files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM