How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service

前端 未结 7 1763
离开以前
离开以前 2020-11-29 20:33

I\'m trying to write a web service client in c# which the webservice is Java Axis 1.4. Axis service requires the Authorization: Basic Base64EncodedToken hea

7条回答
  •  误落风尘
    2020-11-29 20:46

    user334291's answer was a life saver for me. Just want to add how you can add what the OP originally intended to do (what I ended up using):

    Overriding the GetWebRequest function on the generated webservice code:

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        System.Net.WebRequest request = base.GetWebRequest(uri);          
        string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(this.Credentials.GetCredential(uri, "Basic").UserName + ":" + this.Credentials.GetCredential(uri, "Basic").Password));
        request.Headers.Add("Authorization", auth);
        return request;
    }
    

    and setting the credentials before calling the webservice:

      client.Credentials = new NetworkCredential(user, password);       
    

提交回复
热议问题