Adding Authorization Header to Web Reference

前端 未结 3 933

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

3条回答
  •  既然无缘
    2021-02-07 11:53

    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!

提交回复
热议问题