作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在C#中使用HttpClient? 。我曾尝试实现这一点,但没有成功。我被困住了,感谢一些帮助:)
波纹管是一些正在运行的快速代码,然后是显示我尝试做的事情的 C# 代码。提前致谢。
生成 token :
POST https://api.vasttrafik.se/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic UmJseEkyeTFsWVNFTTZ0Z2J6anBTa2E0R1o6Wk1nSkR0Y0paRGV4OTJldUxpQUdYOFExUnU=
grant_type=client_credentials&scope=<device_id>
快速代码:
let data = ("\(Constants.key):\(Constants.secret)").data(using: String.Encoding.utf8)
let base64 = data!.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))
let parameters = [
"grant_type": "client_credentials",
"scope": "\(UIDevice.current.identifierForVendor!.uuidString)"
]
let headers = [
"Authorization": "Basic \(base64)",
"Content-Type": "application/x-www-form-urlencoded"
]
if let expires = UserDefaults.standard.object(forKey: "expires") as? Date, let token = UserDefaults.standard.object(forKey: "token") as? String {
if expires > Date() {
completionHandler(token)
return
}
}
Alamofire.request(Constants.tokenURL, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
if let json = response.result.value as? [String:Any] {
if let token = json["access_token"] as? String, let expires = json["expires_in"] as? Int {
let date = Date()
let expiresTime = Calendar.current.date(byAdding: .second, value: expires, to: date)
UserDefaults.standard.set(expiresTime, forKey: "expires")
UserDefaults.standard.set(token, forKey: "token")
completionHandler(token)
}
}
}
我尝试过这样做,但没有成功。 C# 代码:
var client = new HttpClient();
client.Timeout = new TimeSpan(0, 0, 10);
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_nyckel + ":" + _hemlighet);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(plainTextBytes));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
/*
* Connect to the server
*/
var strin = Injected.Instance.Platform.GetId();
var base54 = System.Convert.ToBase64String(plainTextBytes);
var stringContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials&scope=" + Injected.Instance.Platform.GetId())
});
HttpResponseMessage response = await client.PostAsync(page, stringContent);
response.EnsureSuccessStatusCode();
最佳答案
我使用以下代码解决了这个问题。
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.vasttrafik.se");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("scope", Injected.Instance.Platform.GetId())
});
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(plainTextBytes));
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var result = await client.PostAsync("token",content);
string resultContent = await result.Content.ReadAsStringAsync();
}
关于c# - 如何使用 HttpClient 进行 OAuth2.0 请求 västrafik api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538810/
我是一名优秀的程序员,十分优秀!