【问题标题】:Add parameters to httpclient给httpclient添加参数
【发布时间】:2019-06-30 10:10:13
【问题描述】:

我在 Postman 中编写了一个 HTTP 请求,我想在我的应用程序中编写相同的请求。邮递员中有一个选项可以查看 C# 请求的代码。在邮递员中,它使用RestSharp 显示请求,因为我不想在我的项目中使用外部 NuGet 包,所以我正在尝试使用来自 .NET Framework 的对象编写相同的请求。

RestSharp 代码如下所示:

var client = new RestClient("https://login.microsoftonline.com/04xxxxa7-xxxx-4e2b-xxxx-89xxxx1efc/oauth2/token");
var request = new RestRequest(Method.POST);       
request.AddHeader("Host", "login.microsoftonline.com");            
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "grant_type=password&client_id=6e97fc60-xxx-445f-xxxx-a9b1bbc9eb2d&client_secret=4lS*xxxxxYn%5BENP1p%2FZT%2BpqmqF4Q&resource=https%3A%2F%2Fgraph.microsoft.com&username=myNameHere%402comp.onmicrosoft.com&password=xxxxxxxxxx6", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

我尝试用HttpWebRequest写同样的请求:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/token");
request.Method = "GET";
request.Referer = "login.microsoftonline.com";
request.ContentType = "application/x-www-form-urlencoded";

request.Headers.Add("grant_type", "password");
request.Headers.Add("client_id", "6e97fc60-xxxxxxxxx-a9bxxxxxb2d");
request.Headers.Add("client_secret", "4lSxxxxxxxxxxxmqF4Q");
request.Headers.Add("resource", "https://graph.microsoft.com");
request.Headers.Add("username", "xxxx@xxxxx.onmicrosoft.com");
request.Headers.Add("password", "xxxxxxxxxxxxx");

HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

但是我得到的是 HTML 内容,我认为我需要添加参数而不是作为标题,如何实现?

【问题讨论】:

  • 使用像 wireshark 或 fiddler 这样的嗅探器,并将嗅探器结果与 postman 和 c# 进行比较。 Net 库中的默认标头可能与邮递员不同,这就是您得到不同结果的原因。 Content-Type 可能是造成差异的主要原因,我会添加到您的 c# 代码中。
  • 您不需要将其设置为标题。此处的文档和示例中明确说明了这一点:docs.microsoft.com/en-us/graph/auth-v2-service - 仔细阅读,不要简单地假设事情。

标签: c# httpwebrequest httpclient


【解决方案1】:

如果可以的话,我不会使用WebRequest,而是使用HttpClient

var req = new HttpRequestMessage(HttpMethod.Get, "https://login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/token");
req.Headers.Add("Referer", "login.microsoftonline.com");
req.Headers.Add("Accept", "application/x-www-form-urlencoded");
req.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

// This is the important part:
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
    { "grant_type", "password" },
    { "client_id", "6e97fc60-xxxxxxxxx-a9bxxxxxb2d" },
    { "client_secret", "4lSxxxxxxxxxxxmqF4Q" },
    { "resource", "https://graph.microsoft.com" },
    { "username", "xxxx@xxxxx.onmicrosoft.com" },
    { "password", "xxxxxxxxxxxxx" }
});

HttpResponseMessage resp = await httpClient.SendAsync(req);

// Work with resp

【讨论】:

  • 谢谢它很好用,我只需要更改为httpClient.BaseAddress = new Uri("https://graph.microsoft.com"); httpClient.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
  • @Codey 很高兴能帮助您解决问题,但由于 login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/… 是一个绝对 URL,因此设置 httpClient.BaseAddress 应该没有什么不同对于这个请求,因此可以被删除。
  • @TobiasTengler 如果您的内容是 MutliPart 表单怎么办?然后如何将参数附加到HttpClient
  • 创建一个new MultipartContent(),然后使用您的其他内容调用.Add,例如multiPartContent.Add(new StringContent(bla));。只需搜索 MultipartContent 示例,您应该会找到一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多