添加认证头到Web引用 [英] Adding Authorization Header to Web Reference

查看:195
本文介绍了添加认证头到Web引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发出请求到客户端的Web服务(我不知道在客户端的底层平台)。我一直在使用添加Web引用所消耗的客户端的WSDL在Visual Studio 2010和生成我的代理类(称为的ContactService)。



我现在需要添加一个授权头象下面这样对我的服务请求的人。

 标题=授权和放大器;值=基本12345678901234567890 



(以下简称123456 ......的价值以上仅仅是占位符)

 的ContactService的服务=新的ContactService(); 

//不知道这是正确的方式 - 它不工作
WebClient的客户端=新的WebClient();
client.Headers.Add(授权,基本12345678901234567890);
service.Credentials = client.Credentials;

INT contactKey = NULL;

{
contactKey = service.CreateContact(ABC,EMAILADDRESS,名字,姓氏,NULL);
}



什么是添加授权头到服务请求的正确方法<? / p>

感谢您!


解决方案

上面的反应是正确的轨道上,但它只是必须是在不同的位置。



我添加了这个.NET的产生我的Web引用代理类:

 保护覆盖的WebRequest GetWebRequest(URI URI)
{
HttpWebRequest的REQ =(HttpWebRequest的)base.GetWebRequest(URI);
req.Headers.Add(HttpRequestHeader.Authorization,
基本+12345678901234567890);

返回REQ;
}

一个Web引用代理类扩展System.Web.Services.Protocols.SoapHttpClientProtocol。这个类包含System.Net.WebRequest.GetWebRequest(URI URI)的呼叫。一个WebRequest的让我们调用代理类的方法时,就要求设置特定的头。



感谢您的帮助!


I'm attempting to make requests to a client's web service (I don't know the underlying platform at the client). I've consumed the client's WSDL in Visual Studio 2010 using "Add Web Reference" and generated my proxy class (called "ContactService").

I now need to add an authorization header like the one below to my service request.

Header=Authorization & Value=Basic 12345678901234567890

(the "123456..." value above is just placeholder)

ContactService service = new ContactService();

//not sure if this is the right way - it's not working
WebClient client = new WebClient();
client.Headers.Add("Authorization", "Basic 12345678901234567890");            
service.Credentials = client.Credentials;

int contactKey = null;
try
{                
   contactKey = service.CreateContact("ABC", emailAddress, firstName, lastName, null);
}

What is the proper way of adding the authorization header to the service request?

Thank you!

解决方案

The above response was on the right track, but it just had to be in a different location.

I added this to my web reference proxy class that .Net generated:

protected override WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(uri);            
        req.Headers.Add(HttpRequestHeader.Authorization,
                "Basic " + "12345678901234567890");

        return req;
    }

A Web Reference proxy class extends System.Web.Services.Protocols.SoapHttpClientProtocol. This class contains a call to System.Net.WebRequest.GetWebRequest(Uri uri). A WebRequest allow us to set specific headers on the request when the proxy class' methods are invoked.

Thanks for your help!

这篇关于添加认证头到Web引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆