2012-02-07 204 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请求相当多的工作。