我希望有人可能知道我的問題的原因,而無需我提供 VB6 示例,但如果需要,我可以添加它。我有一個用 C# (.NET 4.5) 和 VB6 編寫的簡單 SOAP 客戶端。C# 代碼使用 WebResponse 來處理我的請求,在 VB6 中我使用 MSXML2.XMLHTTP。它們都在同一臺計算機上運行,具有相同的用戶帳戶,使用完全相同的服務 URL、相同的操作、相同的負載、相同的標頭,并且都不發送任何身份驗證信息。VB6 獲得預期響應,但我的 C# 在調用 WebResponse response = request.GetResponse() 時出現身份驗證錯誤。我不會向任何一個客戶端發送任何形式的身份驗證。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; } }}
1 回答

守候你守候我
TA貢獻1802條經驗 獲得超10個贊
我不知道服務器需要身份驗證。VB6 代碼似乎自動傳遞了登錄用戶的憑據。所以我通過添加以下內容解決了這個問題:
webRequest.UseDefaultCredentials = true;
- 1 回答
- 0 關注
- 152 瀏覽
添加回答
舉報
0/150
提交
取消