我正在尝试通过使用c#
. 我成功地PHP
做到了,但我无法做到C#
我的代码在PHP
下面并且工作正常。
function PS_new_category($n_is_root_category, $n_id_parent, $n_id_parent, $n_active, $n_l_id,$n_l_id_sec, $n_name, $n_desc, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords) {
global $webService;
$xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/categories?schema=blank'));
$resources = $xml -> children() -> children();
unset($resources -> id);
unset($resources -> position);
unset($resources -> id_shop_default);
unset($resources -> date_add);
unset($resources -> date_upd);
$resources -> active = $n_active;
$resources -> id_parent = $n_id_parent;
$resources -> id_parent['xlink:href'] = PS_SHOP_PATH . '/api/categories/' . $n_id_parent;
$resources -> is_root_category = $n_is_root_category;
$node = dom_import_simplexml($resources -> name -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_name));
$resources -> name -> language[0][0] = $n_name;
$resources -> name -> language[0][0]['id'] = $n_l_id;
$resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
//
$resources -> name -> language[1][1] = $n_name;
$resources -> name -> language[1][1]['id'] = 2;//$n_l_id;
$resources -> name -> language[1][1]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . 2;//$n_l_id;
$node = dom_import_simplexml($resources -> description -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_desc));
$resources -> description -> language[0][0] = $n_desc;
$resources -> description -> language[0][0]['id'] = $n_l_id;
$resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
$node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_link_rewrite));
$resources -> link_rewrite -> language[0][0] = $n_link_rewrite;
$resources -> link_rewrite -> language[0][0]['id'] = $n_l_id;
$resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
//
$resources -> link_rewrite -> language[1][1] = $n_link_rewrite;
$resources -> link_rewrite -> language[1][1]['id'] = 2;//$n_l_id;
$resources -> link_rewrite -> language[1][1]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . 2;//$n_l_id;
$node = dom_import_simplexml($resources -> meta_title -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_meta_title));
$resources -> meta_title -> language[0][0] = $n_meta_title;
$resources -> meta_title -> language[0][0]['id'] = $n_l_id;
$resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
//
$resources -> meta_title -> language[1][1] = $n_meta_title;
$resources -> meta_title -> language[1][1]['id'] = $n_l_id_sec;
$resources -> meta_title -> language[1][1]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id_sec;
$node = dom_import_simplexml($resources -> meta_description -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_meta_description));
$resources -> meta_description -> language[0][0] = $n_meta_description;
$resources -> meta_description -> language[0][0]['id'] = $n_l_id;
$resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
$node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection($n_meta_keywords));
$resources -> meta_keywords -> language[0][0] = $n_meta_keywords;
$resources -> meta_keywords -> language[0][0]['id'] = $n_l_id;
$resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;
try {
$opt = array('resource' => 'categories');
$opt['postXml'] = $xml -> asXML();
$xml = $webService -> add($opt);
}
catch (PrestaShopWebserviceException $ex) {
czarodziej_log("PS/SYNCHRONIZACJA KATEGORII: " . $e->getMessage(), 1);
// my log function
echo "<br>LOG: ".$e->getMessage();
}
}
而我的代码,女巫不起作用,c#
这是
private string AddCategoryToEShop(string categoryName)
{
XtraMessageBox.Show("Create New Category");
//PrestaShop.WebService.PrestaShopApi api = PrestashopHelper.CreateNewApiObject();
string url = _eshopUrl;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url + "/api/categories?schema=blank");
request.KeepAlive = true;
request.Credentials = new NetworkCredential(_webServiceUserKey, _webServiceUserPassword);
request.Method = "POST";
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
request.ContentType = "multipart/form-data; boundary=" + boundary;
string postData = @"<category>
<id_parent xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/categories/0'>2</id_parent>
<active>1</active>
<is_root_category>1</is_root_category>
<name><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>New category name C Sharp</language><language id='2' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/2'>Νέα κατηγορία από C Sharp</language></name>
<link_rewrite><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>-dfs-</language><language id='2' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/2'>-dfs-</language></link_rewrite>
<description><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>New desc ...</language><language id='2'/></description>
<meta_title><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>meta-title</language><language id='2' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/2'>meta-title</language></meta_title>
<meta_description><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>meta desc</language><language id='2'/></meta_description>
<meta_keywords><language id='1' xlink:href='http://localhost/dev/clients/Anthagora/Prestashop/trunk/src/api/languages/1'>some,one,keywords</language><language id='2'/></meta_keywords>
</category>";
byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);
//create recuest content
request.ContentLength = postDataBytes.Length;// +boundarybytes.Length;
request.GetRequestStream().Write(postDataBytes, 0, postDataBytes.Length);
// request.GetRequestStream().Write(content, 0, content.Length);
//request.GetRequestStream().Write(boundarybytes, 0, boundarybytes.Length);
string responsText = string.Empty;
try {
WebResponse response = request.GetResponse();
response.Close();
responsText = string.Format("Image of Product {0} added successfully to e-shop", _productId.ToString());
}
catch (Exception ex) {
responsText = ex.ToString();
XtraMessageBox.Show("Exception: (AddCategoryToEShop) " + ex.Message);
}
finally {
}
uploadProgresslistBox.Invoke((MethodInvoker)(() => uploadProgresslistBox.Items.Add(responsText)));
return responsText;
}
谁能帮我??