亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

VB6 的 MSXML2.XMLHTTP 與 C# 的 WebResponse

VB6 的 MSXML2.XMLHTTP 與 C# 的 WebResponse

C#
叮當貓咪 2023-07-09 15:00:53
我希望有人可能知道我的問題的原因,而無需我提供 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;


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號