2012-02-07 687 views
1

我可以從Prestashop API獲取Xml數據,但不能將PUT/POST Xml數據轉換爲PrestaShop API。C#REST API客戶端Prestashop

可能有人建議,我可能會去錯了嗎?

public POST_xml() 
{ 
    Uri address = new Uri("http://.../api/countries/1"); 
    HttpWebRequest request = WebRequest.Create("http://.../api/countries/1") as HttpWebRequest; 

    NetworkCredential("15PJQ4V8CXI22JVW1TKZASDF0OAYNBLA", ""); 

    // Create the web request 
    request = WebRequest.Create(address) as HttpWebRequest; 

    // Set type to POST 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    // Create the data we want to send. 

    string context = "<prestashop><country><id>1</id><id_zone xlink:href=\"http://.../api/zones/1\">"; 
    context += "1</id_zone><id_currency/><iso_code>DE</iso_code><call_prefix>49</call_prefix><active>1</active><contains_states>0</contains_states><need_identification_number>0</need_identification_number><need_zip_code>1</need_zip_code><zip_code_format>NNNNN</zip_code_format><display_tax_label>1</display_tax_label><name><language"; 
    context += " id=\"6\" xlink:href=\"http://.../api/languages/6\">Germanyxx</language></name></country></prestashop>"; 


    StringBuilder data = new StringBuilder(); 
    data.Append("&context=" + HttpUtility.UrlEncode(context)); 


    // Create a byte array of the data we want to send 
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

    // Set the content length in the request headers 
    request.ContentLength = byteData.Length; 

    // Write data 
    using (Stream postStream = request.GetRequestStream()) 
    { 
     postStream.Write(byteData, 0, byteData.Length); 
    } 
} 

回答

1
  1. 的PrestaShop web服務API期望的XML使用XML作爲前綴= POST請求。
  2. 使用一個名爲提琴手程序 - 的Http調試代理,看看到底什麼您是送什麼的web服務回覆。
2

我沒有在c#中導出,但我在Java和VBA中都做了REST調用。你的代碼很少有問題。 我假設您想創建一個全新的國家/地區條目。 那麼你的URL地址應該是

Uri address = new Uri("http://.../api/countries/"); 

而在你的字符串上下文,你不能設置ID爲新資源。

告訴你這裏什麼會爲對資源「的國家/ 1」 PUT請求相當多的工作。