1
我已經使用了WSDL並且已成功稱爲web服務方法。該請求具有隻能在請求發生時添加的授權標頭:向服務引用SOAP請求添加授權標頭
public static NumberCaptureClient Connect()
{
var remoteAddress = new EndpointAddress("https://website.com:8443/webservice/WebServiceNumberCapture");
using (var NumberCaptureClient = new NumberCaptureClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), remoteAddress))
{
NumberCapture.ClientCredentials.UserName.UserName = "test";
NumberCapture.ClientCredentials.UserName.Password = "test";
try
{
using (OperationContextScope scope = new OperationContextScope(NumberCaptureClient.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(NumberCaptureClient.ClientCredentials.UserName.UserName + ":" + NumberCaptureClient.ClientCredentials.UserName.Password));
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
}
}
catch (Exception error)
{
MessageBox.Show("Error");
return null;
}
return NumberCaptureClient;
}
}
正如你所看到的,我需要返回一個代理客戶端的實例(客戶端已經擁有所有需要頭文件的方法),但是需要它,所以頭文件總是被髮送,'using'子句,這是不可能的,因爲範圍在其之外。
有沒有辦法永久添加標題,以便它們隨每個請求發送到webservice?
謝謝,那裏有一點隧道視野。現在完美工作。 –