[英]HTTP Request works in Postman, but not in C# code
我想在 C# 中執行一個簡單的 HTTP 請求,但有些東西不起作用,我得到的只是403 Forbidden
狀態代碼。
當我嘗試在 Postman 中執行相同的請求時,一切正常。 我嘗試運行 Fiddler 並查看 Postman 發送的所有標頭。我復制粘貼了所有標頭,但在 C# 代碼發送的請求中我仍然收到403 Forbidden
。
public static void Main(string[] args)
{
FlurlHttp.Configure(settings => {
settings.HttpClientFactory = new MyClientFactory();
});
var url = "https://example.com"
.AppendPathSegments(new[] { "v1", "oauth", "accesstoken" })
.SetQueryParam("grant_type", "client_credentials")
.AllowAnyHttpStatus()
.WithBasicAuth("username", "password")
.WithHeaders(new {
User_Agent = "Something/0.4.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G975F Build/NRD90M)",
X_Secret_Header = "secret_encoded_value",
accept_encoding = "gzip, deflate",
Accept = "*/*"
});
HttpResponseMessage msg = url.GetAsync().Result;
Console.WriteLine("StatusCodeString: " + msg.StatusCode.ToString());
Console.WriteLine();
Console.WriteLine(msg.Content.ReadAsStringAsync().Result);
}
class MyClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new HttpClientHandler
{
AllowAutoRedirect = false
};
}
}
有人能解釋一下為什么這不起作用嗎? 相同的標頭,相同的一切。
我用“example.com”替換了 url,因為我不想在這里顯示真實的 API URL。
也很抱歉這么多圖片..我不知道如何以其他方式顯示這里的問題。
來自 Postman 的右側應該有一個名為 code 的鏈接。 單擊它,然后選擇 C# 以獲取 Postman 生成的代碼。 把它貼進去試試。
對我來說,問題是 C# 中的 TLS 設置。 嘗試在應用程序的開頭或 HTTP 請求代碼之前添加此行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
只是為了幫助任何人搜索 - 這種錯誤可能有多種原因。
幫助調試此類問題的步驟。
就我而言,我在身份驗證中使用了引號。 這是一個微妙的區別,直到我比較了兩者才意識到
工作電話(200 OK)
呼叫失敗(401 未授權)
下一步是比較來自您的 C# 代碼和 Postman 的原始請求和響應,將它們並排放置並比較差異 - 我向您保證至少會有一個。 :-)
403
是一個授權問題,因此令牌將是第一個嫌疑人,因為您請求的錯誤結構更有可能引發400
“錯誤請求”錯誤。
不過,在這種特殊情況下,我已經在我的機器上使用 Flurl 在 VS2019 中運行了您的代碼,它似乎運行良好。 它返回一個示例 HTML 頁面:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
我的建議是從郵遞員和 C# 應用程序中檢索原始請求字符串,並使用https://text-compare.com/ 之類的東西來查找差異。 猜測是有一些非常小的差異,比如額外的斜線,用普通的眼睛很難注意到。
萬一這對任何人都有幫助。 我在調用 api 時遇到了同樣的問題,並且有點不知所措,因為包括授權標頭在內的所有內容在郵遞員和 Visual Studio 之間似乎完全相同。 但是當我仔細查看 fiddler 時,我注意到在使用郵遞員時,被調用的 api 是https://api.someplace.com/properties/?pagesize=20,而在 Visual Studio 中我有https://api。 someplace.com/properties?pagesize=20
請注意“屬性”和查詢字符串之間缺少斜線。 “...properties/?...” 工作正常,“...properties?...” 導致 403。
我認為(如果我在這里錯了,顯然糾正我)實際發生的事情是在第一個 http 調用中,缺少斜杠,服務器使用斜杠將 HTTP 301 重定向到 api。 當重定向發生時,授權標頭沒有被攜帶,這導致了 403 授權錯誤。
現在為什么這個重定向是必要的我不知道,我構建的任何 apis 都允許這兩種情況並且不需要重定向。 但最后,我只是將 url 更改為“../properties/?etc...”,問題就消失了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.