我正在尝试在c# .net环境中执行以下cURL调用
curl -XPOST -d 'Metadata/Type = "sas"' http://bms.org/bcknd/republishC#代码如下:
var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Metadata/Type", "\"sas\""), });
HttpResponseMessage response = await client.PostAsync("http://bms.org/bcknd/republish", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
Console.WriteLine(await reader.ReadToEndAsync());
}我收到一个400 Bad request,当我把它打印出来的时候。也许它与curl调用中的-XPOST和-d参数有关?
编辑:这是来自curl的http请求:
POST http://bms.org/bcknd/republish HTTP/1.1
Host: bms.org/bcknd
User-Agent: curl/7.48.0
Accept: */*
Content-Length: 43
Content-Type: application/x-www-form-urlencoded
Metadata/Type = "sas"下面是我的代码中的http请求:
POST http://bms.org/bcknd/republish HTTP/1.1
Accept: */*
User-Agent: curl/7.48.0
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: bms.org/bcknd
Content-Length: 43
Connection: Keep-Alive
Metadata/Type = "sas"发布于 2018-12-17 18:45:31
短版
将数据作为不带url编码的StringContent发布,并在尝试读取响应正文之前检查响应状态。请确保调用在应用程序退出之前完成,否则应用程序退出时调用将被取消。这意味着,在Main中使用async Task,而不是async void:
class Program
{
static async Task Main(string[] args)
{
var client=new HttpClient();
var data = new StringContent("Metadata/Type=\"sas\"",Encoding.UTF8,"application/x-www-form-urlencoded");
var response = await client.PostAsync("http://www.google.com/bcknd/republish", data);
if(response.IsSuccessStatusCode)
{
var responseContent = response.Content;
var body=await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
else
{
Console.WriteLine($"Oops! {response.StatusCode} - {response.ReasonPhrase}");
}
}
}说明
在这种情况下,了解实际发送的内容是非常重要的。为此,可以使用像Fiddler或Charles这样的调试代理。
Curl和-d一起发送未编码的数据。此调用:
curl -XPOST -d 'Metadata/Type = "sas"' http://bms.org/bcknd/republish将发送:
POST http://www.google.com/bcknd/republish HTTP/1.1
Host: www.google.com
User-Agent: curl/7.55.1
Accept: */*
Connection: Keep-Alive
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
Metadata/Type = "sas"如果应用了URL编码,/和"将被替换为其他字符。还要注意User-Agent和Accept标头
如果使用--data-urlencode,则将对该值进行URL编码:
POST http://www.google.com/bcknd/republish HTTP/1.1
Host: www.google.com
User-Agent: curl/7.55.1
Accept: */*
Connection: Keep-Alive
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
Metadata/Type =%20%22sas%22另一方面,这段代码:
static async Task Main(string[] args)
{
var client=new HttpClient();
var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Metadata/Type", "\"sas\""), });
var response = await client.PostAsync("http://www.google.com/bcknd/republish", data);
var responseContent = response.Content;
var body=await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}将发送:
POST http://www.google.com/bcknd/republish HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 25
Host: www.google.com
Metadata%2FType=%22sas%22要获得原始有效负载,可以使用带有手动编码内容的StringContent:
var data = new StringContent("Metadata/Type= \"sas\"",Encoding.UTF8,"application/x-www-form-urlencoded");请求是:
POST http://www.google.com/bcknd/republish HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 19
Host: www.google.com
Metadata/Type= "sas"如果要发送User-Agent和Accept标头,可以将它们添加到每条单独的消息中或作为默认的请求标头:
var client=new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("curl","7.55.1"));这些将添加:
Accept: */*
User-Agent: curl/7.55.1对请求
发布于 2018-12-17 17:47:22
您可以使用HttpClient调用远程URL,如下所示
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://bms.org/bcknd/republish"))
{
request.Content = new StringContent("Metadata/Type = \"sas\"", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
}
}在这里,我刚刚添加了参考代码,通过使用它,您可以创建自己的代码。我检查了你的curl请求和it seems issue it self。
https://stackoverflow.com/questions/53812191
复制相似问题