我希望有人可能知道我的問(wèn)題的原因,而無(wú)需我提供 VB6 示例,但如果需要,我可以添加它。我有一個(gè)用 C# (.NET 4.5) 和 VB6 編寫(xiě)的簡(jiǎn)單 SOAP 客戶(hù)端。C# 代碼使用 WebResponse 來(lái)處理我的請(qǐng)求,在 VB6 中我使用 MSXML2.XMLHTTP。它們都在同一臺(tái)計(jì)算機(jī)上運(yùn)行,具有相同的用戶(hù)帳戶(hù),使用完全相同的服務(wù) URL、相同的操作、相同的負(fù)載、相同的標(biāo)頭,并且都不發(fā)送任何身份驗(yàn)證信息。VB6 獲得預(yù)期響應(yīng),但我的 C# 在調(diào)用 WebResponse response = request.GetResponse() 時(shí)出現(xiàn)身份驗(yàn)證錯(cuò)誤。我不會(huì)向任何一個(gè)客戶(hù)端發(fā)送任何形式的身份驗(yàn)證。C#代碼如下:using System.Net;using System.IO;using System.Text;namespace CarryUpReport{ public class PricedexHttpClient { public void Send(string url, string action, string body) { HttpWebRequest request = CreateWebRequest(url, action); request.AllowWriteStreamBuffering = false; // see: https://support.microsoft.com/en-us/help/908573/a-post-or-put-request-may-fail-when-you-use-the-httpwebrequest-class-t byte[] byteArray = Encoding.UTF8.GetBytes(body); request.ContentLength = byteArray.Length; Stream stream = null; try { stream = request.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); // the next line throws an exception: using (WebResponse response = request.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { string soapResult = rd.ReadToEnd(); } } } finally { if (null != stream) stream.Dispose(); } } public static HttpWebRequest CreateWebRequest(string url, string action) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Headers.Add(@"SOAPAction", action); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } }}
VB6 的 MSXML2.XMLHTTP 與 C# 的 WebResponse
叮當(dāng)貓咪
2023-07-09 15:00:53