我試圖使用WCF向一個API的soap發送請求,在API檔案中我被告知,首先我需要傳遞以下包含固定令牌的認證頭:
<soapenv:Header>
<Token xmlns="Token"> 12345as566788ds900987654</Token>
</soapenv:Header>
在傳遞和驗證這個令牌后,我訪問了我需要發送檔案的類,我嘗試用下面的代碼,我設法組裝搜索,但我得到了錯誤。System.ServiceModel.FaultException: 通知我需要在標題中傳遞token標簽。 下面是我的嘗試:
using (new OperationContextScope(client.Innerchannel))
{
HttpRequestMessageProperty requestMessage = new()。
requestMessage.Headers["Token"/span>] = "12345as566788ds900987654"/span>;
var result= client.uploadFile(file)。
}
uj5u.com熱心網友回復:
你可以嘗試在客戶端這樣做:
IContextChannel contextChannel = (IContextChannel)myServiceProxy;
using (OperationContextScope scope = new OperationContextScope(contextChannel))
{
MessageHeader header = MessageHeader.CreateHeader("PlayerId", "", _playerId)。)
OperationContext.Current.OutgoingMessageHeaders.Add(header)。
act(service)。
}
而你可以在服務器端嘗試這樣做:
private long ExtractPlayerIdFromHeader())
{
try
{
var opContext = OperationContext.Current。
var requestContext = opContext.RequestContext。
var headers = requestContext.RequestMessage.Headers;
int headerIndex = headers.FindHeader("PlayerId", "") 。
long playerId = headers.GetHeader<long>(headerIndex)。
return playerId。
}
catch (Exception ex)
{
this.Log.Error("Exception thrown when extracting the player id from the header"/span>, ex)。
throw;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316158.html
標籤:
上一篇:正則運算式使逗號分隔的數字無效
