如何发送一个HTTP基本认证后? [英] how to send a http basic auth post?
问题描述
我一直在考虑创建一个使用基本身份验证一个HTTP POST任务。
我在C#开发的一个asp.net MVC应用程序。
I have been given the task to create a http post using basic auth. I am developing in C# in an asp.net MVC application.
我也被赋予了这个例子。
I have also been given this example.
{
POST /v2/token_endpoint HTTP/1.1
Authorization: Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=
Accept: application/json
Content-Type: application/x-www-form-urlencoded
User-Agent: Java/1.6.0_33
Host: api.freeagent.com
Connection: close
Content-Length: 127
grant_type=authorization_code&code=12P3AsFZXwXjd7SLOE1dsaX8oCgix&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth
}
我的问题是我如何code这在C#?
如果有更多的信息,只需要问,在此先感谢
My question is how do i code this in C#? If more information is need just ask, thanks in advance
编辑:我已经取得了一些进展,但我还没有加入grant_type
edit: I have made some progress but I have not added the grant_type
public void AccessToken(string code)
{
string url = @"https://api.freeagent.com/v2/token_endpoint";
WebClient client = new WebClient();
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(ApiKey + ":" + ApiSecret));
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
client.Headers[HttpRequestHeader.UserAgent] = "Java/1.6.0_33";
client.Headers[HttpRequestHeader.Host] = "api.freeagent.com";
client.Headers[HttpRequestHeader.Connection] = "close";
client.Headers["grant_type"] = "authorization_code";
var result = client.DownloadString(url);
}
让我怎么加: grant_type=authorization_$c$c&$c$c=12P3AsFZXwXjd7SLOE1dsaX8oCgix&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth该职位?
So how do i add: grant_type=authorization_code&code=12P3AsFZXwXjd7SLOE1dsaX8oCgix&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth to the post?
推荐答案
您可以在这里找到两个样本如何使的WebRequest和WebClient的类基本身份验证的要求:
You can find here two samples how to make basic auth request with WebRequest and WebClient classes:
<一个href=\"http://grahamrhay.word$p$pss.com/2011/08/22/making-a-post-request-in-c-with-basic-authentication/\" rel=\"nofollow\">http://grahamrhay.word$p$pss.com/2011/08/22/making-a-post-request-in-c-with-basic-authentication/
的http://anishshenoy57.word$p$pss.com/2013/01/22/basic-http-authentication-using-c/
基本上基本身份验证,它只是的Base64(用户名:密码)
,所以很容易实现它。
Basically basic auth it's just Base64(username:password)
, so it's easy to implement it.
UPDATE1
下面是根据你的方法的例子:
Here is a sample based on your method:
public void AccessToken(string code)
{
string url = @"https://api.freeagent.com/v2/token_endpoint";
WebClient client = new WebClient();
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(ApiKey + ":" + ApiSecret));
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
client.Headers[HttpRequestHeader.UserAgent] = "Java/1.6.0_33";
client.Headers[HttpRequestHeader.Host] = "api.freeagent.com";
client.Headers[HttpRequestHeader.Connection] = "close";
client.Headers["grant_type"] = "authorization_code";
string data = string.Format(
"grant_type=authorization_code&code={0}&redirect_uri=http%3A%2F%2Flocalhost%3A8080",
code);
var result = client.UploadString(url, data);
}
在调用方法 Web客户端的唯一不同
。 DownloadString会做 GET
的要求,但对 POST
你需要使用的 UploadString
法
The only different in calling method in WebClient
. DownloadString will do GET
request, but for POST
you need to use UploadString
method
这篇关于如何发送一个HTTP基本认证后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!