2015-07-19 95 views
0

我希望在發送請求之前爲我的httpclient對象擁有多個自定義請求標頭條目。我想發送請求應該是這樣的:使用HttpClient的多個自定義請求標頭條目

Request Headers  
Connection:keep-alive 
Content-Length:213 
Content-Type:application/json  
Host:portal.idtbeyond.com 
Origin:https://portal.idtbeyond.com 
Referer:https://portal.idtbeyond.com/activedocs 
x-idt-beyond-app-id:xxccccxx 
x-idt-beyond-app-key:yyuuuttttddfdfdfdfdfd 
X-Requested-With:XMLHttpRequest 
Request Payload 
view parsed 
{ 
    "country_code": "SV", 
    "carrier_code": "Claro", 
    "mobile_number": "50363751234", 
    "plan": "Sandbox", 
    "amount": "500", 
    "client_transaction_id": "", 
    "terminal_id": "KIOSK 1", 
    "origin_country": "US" 
} 

我寫在C#中的代碼是如下:

using (var client = new HttpClient()) 
{ 
    client.BaseAddress = new Uri(_ProviderService.GatewayUrl); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json")); 
    client.DefaultRequestHeaders.Add("x-idt-beyond-app-id", 
     _PayingMerchant.Username); 
    client.DefaultRequestHeaders.Add("x-idt-beyond-app-key", 
     _PayingMerchant.Password); 
    var urlSuffix = GetUrlSuffix(trans);     

    HttpResponseMessage response = client.GetAsync(urlSuffix).Result; 
    if (response.IsSuccessStatusCode) 
     { 
      TopUpResponse topUpResponse = 
       response.Content.ReadAsAsync<TopUpResponse>().Result; 
      if (topUpResponse.success) 
      { 
       result.Success = true; 
       result.ReturnValue = true; 
      } 
     } 
    } 

我得到一個404響應和調試表明,頭節對我來說錯了。我對這些請求不熟悉,並希望得到任何幫助。

問候

+2

404通常表明你正在試圖通過向不特定服務存在;如果它是一個頭的問題,通常會返回一個400錯誤的請求或替代錯誤...您確定404因爲頭返回嗎? –

+0

感謝您的回覆,我遇到的問題是多個標題,當我查看我的網絡時 - X-idt params被附加到標題的接受部分。註釋掉兩個x-idt參數將返回來自網關的未經授權的響應。我已驗證網關網址及其正確 – Dzidzai

回答

0

嘗試設置引薦以及如下:

client.DefaultRequestHeaders.Referer = https://portal.idtbeyond.com/activedocs 

並確保您使用的是正確的GatewayUrl

+0

感謝回覆,我遇到的問題是多個標題,當我查看網絡時 - X-idt params被附加到標題的接受部分。註釋掉兩個x-idt參數將返回來自網關的未經授權的響應。我已驗證網關網址及其正確性 – Dzidzai

相關問題