为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信