使用 Web 参考进行 HTTP 身份验证

发布于 2024-07-21 06:20:43 字数 841 浏览 6 评论 0原文

我有一个从 WSDL 创建的 Web 引用,但除非传入用户名/密码,否则不允许调用该函数; XML 工具包的原始代码是:

Set client = CreateObject("MSSOAP.SOAPClient30")
URL = "http://" & host & "/_common/webservices/Trend?wsdl"

client.mssoapinit (URL)

client.ConnectorProperty("WinHTTPAuthScheme") = 1
client.ConnectorProperty("AuthUser") = user
client.ConnectorProperty("AuthPassword") = passwd

On Error GoTo err
Dim result1() As String

result1 = client.getTrendData(expression, startDate, endDate, 
              limitFromStart, maxRecords

How do I add the AuthUser/AuthPassword to my new code?

新代码:

    ALCServer.TrendClient tc = new WindowsFormsApplication1.ALCServer.TrendClient();

    foreach(string s in tc.getTrendData(textBox2.Text, "5/25/2009", "5/28/2009", false, 500))
        textBox1.Text+= s;

I have a web reference created from the WSDL, but I'm not allowed to call the function unless I pass in the username / password; the original code for the XML toolkit was:

Set client = CreateObject("MSSOAP.SOAPClient30")
URL = "http://" & host & "/_common/webservices/Trend?wsdl"

client.mssoapinit (URL)

client.ConnectorProperty("WinHTTPAuthScheme") = 1
client.ConnectorProperty("AuthUser") = user
client.ConnectorProperty("AuthPassword") = passwd

On Error GoTo err
Dim result1() As String

result1 = client.getTrendData(expression, startDate, endDate, 
              limitFromStart, maxRecords

How do I add the AuthUser/AuthPassword to my new code?

New code:

    ALCServer.TrendClient tc = new WindowsFormsApplication1.ALCServer.TrendClient();

    foreach(string s in tc.getTrendData(textBox2.Text, "5/25/2009", "5/28/2009", false, 500))
        textBox1.Text+= s;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-07-28 06:20:43

发现:即使Preauthenticate==True,它也不会这样做。 您必须覆盖 WebRequest:

   protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);

        if (PreAuthenticate)
        {
            NetworkCredential networkCredentials =
                Credentials.GetCredential(uri, "Basic");

            if (networkCredentials != null)
            {
                byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                    networkCredentials.UserName + ":" +
                    networkCredentials.Password);
                request.Headers["Authorization"] =
                    "Basic " + Convert.ToBase64String(credentialBuffer);
            }
            else
            {
                throw new ApplicationException("No network credentials");
            }
        }
        return request;
    }

由于它是作为分部类创建的,因此您可以将存根保留在单独的文件中,并且重建 Reference.cs 不会让您感到困扰。

Found it: Even if Preauthenticate==True, it doesn't do it. You have to overried the WebRequest:

   protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);

        if (PreAuthenticate)
        {
            NetworkCredential networkCredentials =
                Credentials.GetCredential(uri, "Basic");

            if (networkCredentials != null)
            {
                byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                    networkCredentials.UserName + ":" +
                    networkCredentials.Password);
                request.Headers["Authorization"] =
                    "Basic " + Convert.ToBase64String(credentialBuffer);
            }
            else
            {
                throw new ApplicationException("No network credentials");
            }
        }
        return request;
    }

Since it gets created as a partial class, you can keep the stub in a separate file and rebuilding the Reference.cs won't clobber you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文