[英]How to call web API URL in C# windows application and get response
在Windows應用程序中調用API URL時遇到問題。 我的代碼如下。
var client1 = new RestClient("http://stagingapi.bulkmro.com/index.php/product/736387");
var request1 = new RestRequest(Method.GET);
request1.AddHeader("postman-token", "79dbc057-2279-0d13-9d98-484efbdce8da");
request1.AddHeader("cache-control", "no-cache");
//request1.AddHeader("authorization", "Api RjNOZmVxRFluSkY1VmxSOVBoaHVRM1lPTmZWeFFyRUI4ZVBWUWhMTA==");
request1.AddHeader("authorization", "API '" + key + "'");
IRestResponse response1 = client1.Execute(request1);
string str222 = response1.ToString();
string aa1 = response1.Content.ToString();
var jss1 = new JavaScriptSerializer();
var dict1 = jss1.Deserialize<Dictionary<string, string>>(aa1);
string key1 = dict1["value_id"];
但它的回應是未經授權的。 請提出任何答案。
看來該API正在使用基本訪問身份驗證 。 通常,對於BAA,標頭密鑰為Authorization
,其值為Basic {base64 hash}
。 在您的示例中,您似乎將值設置為API {base64 hash}
。 您確定API期望值是“ API”而不是“ Basic”嗎?
另外,在您的代碼中:
request1.AddHeader("authorization", "API '" + key + "'");
您實際上是在哈希周圍發送單引號。 通常情況並非如此。 您實際上是在發送API '{base64 hash}'
。 您將需要刪除“單引號”。 像這樣:
request1.AddHeader("authorization", "API " + key);
如果這不起作用,請嘗試以下操作:
request1.AddHeader("authorization", "Basic " + key);
試試這個,request1.AddHeader(“ authorization”,“ Basic” + key); ..它一定會工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.