不知何故,我最终遇到了使用SOAP端点和运行vb的aspx页面的问题。 发送文件和简单数据有效,但我无法再次下载文件。
对于 MTOM 响应,我只是正则表达式中的内容并将其解析为 XElement,它有效。现在对于二进制流,我尝试了相同的方法,但尚未成功。请参阅下面的代码,以调用终结点并获取结果。
Private Function sendSOAPRequest(url As String, enpoint As String, data As Byte()) As String
Dim result As String = ""
Try
Dim req = WebRequest.Create(url)
req.Headers.Add("SOAPAction", url + enpoint)
req.ContentType = "application/soap+xml"
req.ContentLength = data.Length
req.Method = "POST"
Dim DataStream = req.GetRequestStream()
DataStream.Write(data, 0, data.Length)
DataStream.Close()
Dim WebResponse = req.GetResponse()
Dim resp = WebResponse.GetResponseStream()
Dim Reader = New StreamReader(resp)
result = Reader.ReadToEnd()
DataStream.Close()
Reader.Close()
resp.Close()
Catch ex As WebException
System.Diagnostics.Debug.WriteLine(ex.ToString())
End Try
System.Diagnostics.Debug.WriteLine(result)
Return result
End Function
现在对于登录/注销/上传,这有效。现在对于一些文件内容调用,我得到了这个:
HTTP/1.1 200 OK
X-Powered-By: Undertow/1
Access-Control-Allow-Headers: accept, authorization, content-type, x-requested-with
Server: WildFly/10
Date: Fri, 12 Apr 2019 12:06:58 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:6c8ac7c2-cac9-437f-a319-72c7c0e60328"; start="<root.message@cxf.apache.org>"; start-info="application/soap+xml"
Content-Length: 5371
Access-Control-Max-Age: 1
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT
--uuid:6c8ac7c2-cac9-437f-a319-72c7c0e60328
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><ns2:getContentObjectStreamResponse xmlns:ns2="http://ws.soap.transfer.services.sedna.ser.com/"><dataHandler><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:a31b1e84-b3a5-4482-90af-f99943ec08a2-9@cxf.apache.org"/></dataHandler></ns2:getContentObjectStreamResponse></soap:Body></soap:Envelope>
--uuid:6c8ac7c2-cac9-437f-a319-72c7c0e60328
Content-Type: */*
Content-Transfer-Encoding: binary
Content-ID: <a31b1e84-b3a5-4482-90af-f99943ec08a2-9@cxf.apache.org>
‰PNG
binary data
--uuid:6c8ac7c2-cac9-437f-a319-72c7c0e60328--
如何仅解析二进制部分并取回文件?
更新16.04.2019:我得到了以下几行的内容:
Dim rContent As New System.Text.RegularExpressions.Regex("(?<=Content-ID: <[^root].*[>])(.|n)*(?=--uuid)")
Dim matchContent As Match = rContent.Match(documentResponse)
Dim documentContentData As String = matchContent.Value
但似乎字符串转换以某种方式弄乱了二进制编码。将该内容写入文件
Dim fileBytes As Byte() = UnicodeStringToBytes(trimmedContent)
File.WriteAllBytes("C:Pathcloud.png", fileBytes)
比原始文件大,编码错误。
我找到了一个既不优雅也不值得推荐的解决方案。解析每个字节的响应字节,并通过剪切信封和附上消息(反复试验(来复制二进制部分。现在,让我们只希望端点永远不会更改此格式。
Private Function getSoapFileBytes(url As String, endpoint As String, data As Byte()) As Byte()
Dim result As String = ""
Dim fileBytes As New List(Of Byte)
Try
Dim req = WebRequest.Create(url)
req.Headers.Add("SOAPAction", url + endpoint)
req.ContentType = "application/soap+xml"
req.ContentLength = data.Length
req.Method = "POST"
Dim DataStream = req.GetRequestStream()
DataStream.Write(data, 0, data.Length)
DataStream.Close()
Dim WebResponse = req.GetResponse()
Dim resp = WebResponse.GetResponseStream()
Dim contentList As New List(Of Byte)
Dim ct As Byte() = New Byte() {}
Dim currentByte As Integer = 0
While currentByte > -1
currentByte = resp.ReadByte()
Dim myByte = BitConverter.GetBytes(currentByte)
ct.Concat(myByte)
contentList.Add(myByte.ElementAt(0))
End While
Dim fb As Byte() = New Byte() {}
For c As Integer = 772 To (contentList.Count - 49)
fileBytes.Add(contentList.ElementAt(c))
Next
DataStream.Close()
resp.Close()
Catch ex As WebException
System.Diagnostics.Debug.WriteLine(ex.ToString())
End Try
System.Diagnostics.Debug.WriteLine(result)
Return fileBytes.ToArray()
End Function