2011-11-08 83 views
0

我試圖做的在C#以下的等價...這個cURL PUT請求的C#等價物是什麼?

curl --request PUT \ 
    --user-agent "Your Client Name/1.0" \ 
    --header "Authorization: Basic YWRtaW46WW91ckFQSUtleUhlcmU=" \ 
    --header "Content-Type: application/xml" \ 
    --data-binary '<order><status_id>10</status_id></order>' \ 
    https://www.example.com/api/v2/orders/101 

我的努力給我的404 - 錯誤的請求響應。我從來沒有看過捲曲。 任何人有任何想法?

var url = String.Format("https://www.website.com/api/v2/orders/{0}", 1647); 
var credentials = Convert.ToBase64String(
       Encoding.ASCII.GetBytes(String.Format("{0}:{1}", "admin", "mypassword"))); 
var request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "PUT"; 
request.Headers.Add(String.Format("Authorization: Basic {0}", credentials)); 

var status = "<?xml version='\"1.0'\" encoding='\"UTF-8'\"?><order><status_id>2<order><status_Id>"; 

using (var stream = request.GetRequestStream()) 
{ 
    using (var writer = new BinaryWriter(stream)) 
    { 
     writer.Write(status); 
    } 
} 

var response = request.GetResponse(); 
+0

不要使用BinaryWriter.Write(字符串),它首先寫入字符串長度。使用StreamWriter。 –

回答

1

通常是 '錯誤的請求' 是因爲頭格式:

看看

<?xml version='\"1.0'\" encoding='\"UTF-8'\"?> 

應該寫成遵循

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 

沒有「,或者你想避免你可以寫的逃生

<?xml version='1.0' encoding='UTF-8'?> 
相關問題