gpt4 book ai didi

c# - VB 6's MSXML2.XMLHTTP vs C#' s WebResponse

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:32 32 4
gpt4 key购买 nike

我希望有人可能知道我的问题的原因而不必提供我的 VB6 示例,但我可以在必要时添加它。

我在 C# (.NET 4.5) 和 VB6 中都有一个简单的 SOAP 客户端。 C# 代码使用 WebResponse 来处理我的请求,而在 VB6 中我使用的是 MSXML2.XMLHTTP。

它们都在同一台机器上运行,使用相同的用户帐户,使用完全相同的服务 URL、相同的操作、相同的负载、相同的 header ,并且都不发送任何身份验证信息。 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;
}
}
}

最佳答案

我不知道服务器需要身份验证。似乎 VB6 代码自动传递了登录用户的凭据。所以我通过添加解决了这个问题:

webRequest.UseDefaultCredentials = true;

关于c# - VB 6's MSXML2.XMLHTTP vs C#' s WebResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120658/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com