不知何故,我最终遇到了使用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
现在登录/注销/上传这个工作。现在对于一些fileContent调用我得到了回复:
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="<[email protected]>"; 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: <[email protected]>
<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:[email protected]"/></dataHandler></ns2:getContentObjectStreamResponse></soap:Body></soap:Envelope>
--uuid:6c8ac7c2-cac9-437f-a319-72c7c0e60328
Content-Type: */*
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
‰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
但似乎String转换以某种方式混淆了二进制编码。将该内容写入文件
Dim fileBytes As Byte() = UnicodeStringToBytes(trimmedContent)
File.WriteAllBytes("C:\Path\cloud.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