请求协助将SOAP POST请求转换为VB.NET代码并排查远程服务器无响应问题
Let's walk through the issues in your code and get that SOAP request working properly:
Key Issues in Your Current Code
Here are the main problems causing the "cannot get response from remote server" error:
Incorrect Request URL
You're sending the request tohttps://www.service.velocify.com, but the correct endpoint (per the supplier's spec) ishttps://service.velocify.com/ClientService.asmx— you missed the/ClientService.asmxpath and added an extrawww.which likely doesn't point to the right service.Missing SOAPAction Header
The SOAPAction header is required for this service, and you've commented out the line that sets it. The correct value matches what the supplier provided:"https://service.leads360.com/ModifyLeadStatus".Mismatched SOAP Version
Your code uses SOAP 1.2 (xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"), but the supplier's example uses SOAP 1.1 (xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"). This mismatch will cause the server to reject your request.Wrong Data Type for
statusId
The supplier definesstatusIdas an integer, but your XML has<statusId>string</statusId>— this will cause a validation error on the server side.Broken Response Handling
You've commented out the code that retrieves the server response, and yourStreamReaderis trying to read from the already-closed request stream (not the response stream).
Corrected VB.NET Code
Here's the fixed version of your function with all these issues addressed:
Module Velocify ' Return a boolean to indicate success/failure Function SOAPRequest(username As String, password As String, leadId As Integer, statusId As Integer) As Boolean Dim request As WebRequest Dim response As WebResponse = Nothing Dim dataStream As Stream = Nothing Dim reader As StreamReader = Nothing Dim soapByte() As Byte Dim soapStr As String Dim isSuccess As Boolean = True ' Build the SOAP XML with correct SOAP 1.1 namespace and valid values soapStr = "<?xml version=""1.0"" encoding=""utf-8""?>" & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & "<soap:Body>" & "<ModifyLeadStatus xmlns=""https://service.leads360.com"">" & $"<username>{username}</username>" & $"<password>{password}</password>" & $"<leadId>{leadId}</leadId>" & $"<statusId>{statusId}</statusId>" & "</ModifyLeadStatus>" & "</soap:Body>" & "</soap:Envelope>" Try ' Convert SOAP string to bytes soapByte = System.Text.Encoding.UTF8.GetBytes(soapStr) ' Correct request URL request = WebRequest.Create("https://service.velocify.com/ClientService.asmx") ' Add required SOAPAction header request.Headers.Add("SOAPAction", "https://service.leads360.com/ModifyLeadStatus") ' Set correct content type and method request.ContentType = "text/xml; charset=utf-8" request.ContentLength = soapByte.Length request.Method = "POST" ' Write the SOAP data to the request stream dataStream = request.GetRequestStream() dataStream.Write(soapByte, 0, soapByte.Length) dataStream.Close() ' Get and read the server response response = request.GetResponse() dataStream = response.GetResponseStream() reader = New StreamReader(dataStream) Dim responseText As String = reader.ReadToEnd() ' Optional: Log or process the response text here Console.WriteLine("Server Response: " & responseText) Catch ex As WebException ' More detailed error handling (check response status if available) If ex.Response IsNot Nothing Then Using errorReader As New StreamReader(ex.Response.GetResponseStream()) MsgBox("Error Response: " & errorReader.ReadToEnd()) End Using Else MsgBox("Request Error: " & ex.ToString()) End If isSuccess = False Catch ex As Exception MsgBox("Unexpected Error: " & ex.ToString()) isSuccess = False Finally ' Clean up resources to avoid leaks reader?.Close() dataStream?.Close() response?.Close() End Try Return isSuccess End Function End Module
Additional Notes
- I've updated the function to accept parameters for
username,password,leadId, andstatusIdinstead of hardcoding placeholder values — this makes it reusable. - Added proper resource cleanup in a
Finallyblock to prevent memory leaks. - Enhanced error handling to read the server's error response (if available), which will help you debug any future issues with the request.
- Used string interpolation (
$"") for cleaner XML construction.
内容的提问来源于stack exchange,提问作者cowboycodering

