[英]cURL call in c# bad request
我正在嘗試在ac#.net環境中進行以下cURL調用
curl -XPOST -d 'Metadata/Type = "sas"' http://bms.org/bcknd/republish
C#代碼如下:
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錯誤的請求,並且在我打印出來時。 也許與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"
簡潔版本
嘗試將數據作為StringContent
而不使用url編碼發布,並在嘗試讀取響應正文之前檢查響應狀態。 確保在應用程序退出之前完成呼叫,否則在應用程序退出時將取消呼叫。 這意味着在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之類的調試代理。
帶-d
Curl發送未編碼的數據 。 這個電話:
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
根據要求
您可以使用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);
}
}
在這里,我剛剛添加了參考代碼,使用它可以創建自己的參考代碼。 我檢查了您的卷曲要求, 看來是自我發出的 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.