[英]403 Forbidden error using Webrequest in C# but works in postman
我有一個授權代碼,我在調用api時需要在主體中傳遞一些標頭值。 當嘗試從郵遞員相同時,它的工作正常,但C#Webclient拋出403錯誤。
代碼如下:-
公共字符串GetResponse(字符串AuthCode){
string url = "https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa";
Uri uri = new Uri(string.Format(url));
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "code=" + AuthCode + "&redirect_uri=" + "http://localhost:8080";
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);
// Create the request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + "MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
// Get the response
HttpWebResponse httpResponse = null;
try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine("Exception while getting resource " + ex.Message);
return null;
}
string result = null;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
郵遞員卷曲命令:-
從curl請求生成:
卷曲-X POST ' https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa ' -H '授權:基本MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc =' -H '緩存控制:無緩存' -H'內容類型:application / x-www-form-urlencoded'-d'code = 93317468-7464-4804-b38a-43e13265c4ac&redirect_uri = http%3A%2F%2Flocalhost%3A8080%2F'
我無法弄清楚問題所在。 誰能幫幫我嗎
使用RestSharp並傳遞正確的標頭值解決了問題
var client = new RestClient("https://example.com/openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Basic MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
request.AddParameter("undefined", "code=" + AuthCode + "&redirect_uri=http%3A%2F%2Flocalhost%3A8080", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(response.Content)))
{
// Deserialization from JSON
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Token));
Token token = (Token)deserializer.ReadObject(ms);
return userinfo= GetuserInfo(token.id_token);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.