2013-04-25 104 views
16

我想使用这个API与C#应用程序: http://www.affjet.com/2012/11/26/4-4-affjet-api/#more-3099传递参数作为一个Object []

添加WSDL来我projcet后,我写了这个简单的代码: (getTransactions得到一个对象[] @params并返回一个字符串)

Ws_ApiService service = new Ws_ApiService(); 
string apiKey = "*************"; 
var response = service.getTransactions(new object[] { apiKey }); 

我试过几个方法,但不可能得到正确的反应, 我想:

var response = service.getTransactions(new object[] { "apiKey:****"}); 

var response = service.getTransactions(new object[] { "apiKey","******"}); 

这里是PHP代码,做同样的从自己的文件:

<?php 

$nameSpace = "https://secure.affjet.com/ws/api"; 

//Creating AffJet client for SOAP 
$client = new SoapClient($nameSpace."?wsdl"); 

$pageNumber = 0; 
//Setting up parameters 
$params = array(); 
$params["apiKey"] = "MY_API_KEY"; 
//Value for parameters (optional) 
//$params["networkId"] = array(1,2); 
//$params["pageNumber"] = 0; 
//$params["pageSize"] = 10; 
//Making Request 
$response = $client->getNetworks($params); 
//XML to SimpleXMLElement Object 
$xmlResponse = new SimpleXMLElement($response); 
if ($xmlResponse->success == "true"){ 
    while (isset($xmlResponse->dataList->data)) { 
     //Iterate the results 
     foreach ($xmlResponse->dataList->data as $data){ 
      var_dump(xml2array($data)); 
     } 
     //Requesting next page of data 
     $pageNumber++; 
     $params["pageNumber"] = $pageNumber; 
     //Making Request 
     $response = $client->getNetworks($params); 
     //XML to SimpleXMLElement Object 
     $xmlResponse = new SimpleXMLElement($response); 
    } 
} else { 
    //Error somewhere 
    echo $xmlResponse->errorMessage; 
} 

/** 
* Transforms the object SimpleXmlElement into an array, easier to handle 
*/ 
function xml2array($xml) { 
    $arr = array(); 
    foreach ($xml as $element) { 
     $tag = $element->getName(); 
     $e = get_object_vars($element); 
     if (!empty($e)) { 
      $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e; 
     } else { 
      $arr[$tag] = trim($element); 
     } 
    } 
    return $arr; 
} 


?> 

这是什么,我尝试了回应:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://secure.affjet.com/ws/api" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
      SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Body> 
     <ns1:getTransactionsResponse> 
     <return xsi:type="xsd:string"> 
      &lt;response&gt;&lt;success&gt;false&lt;/success&gt;&lt;errorMessage&gt; 
      API Key not provided&lt;/errorMessage&gt;&lt;dataList&gt; 
      &lt;/dataList&gt;&lt;/response&gt; 
     </return> 
     </ns1:getTransactionsResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

你可以看到:

API Key not provided 

A第二反应应该是这样的:

<response> 
    <success>true</success> 
    <errorMessage></errorMessage> 
    <dataList> 
     <data> 
      <date>2012-11-05 15:02:41</date>//Transaction Date 
      <amount>81.67</amount> 
      <commission>15.86</commission> 
      <status>confirmed</status>//Status, could be: declined, pending, confirmed or paid 
      <clickDate></clickDate> 
      <ip></ip> 
      <custom_id>MyCustomId</custom_id>//Custom Id for the transactions (SID, SubId,clickRef....) 
      <unique_id>2548462</unique_id>//Unique Id given from the network to this transaction 
      <merchantId>1</merchantId>//Id for the Merchant on AffJet 
      <networkId>1</networkId>//Id for the Network on AffJet 
     </data> 
    </dataList> 
</response> 

所有我需要提供一个名为“apiKey”参数,并将其值

编辑:

联系他们的支持后,他们说的要求应该看起来像这样:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ns1="http://secure.affjet.com/ws/api" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:ns2="http://xml.apache.org/xml-soap" 
        xmlns:SOAP- ENC="http://schemas.xmlsoap.org/soap/encoding/" 
      SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Body> 
     <ns1:getTransactions> 
      <params xsi:type="ns2:Map"> 
       <item> 
        <key xsi:type="xsd:string">apiKey</key> 
        <value xsi:type="xsd:string">YOURAPIKEY</value> 
       </item> 
      </params> 
     </ns1:getTransactions> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

任何想法?

+1

发生了什么事你试过吗?返回一个字符串?字符串是什么?你期望它是什么? – 2013-04-25 17:27:33

+0

我编辑了你所要求的:) – 2013-04-25 17:32:19

+0

你可以看到service.getTransactions()的sig方法吗? – 2013-04-25 17:37:33

回答

0

看来问题出在PHP和C#中的数组之间的区别。在PHP中,它是一个关键值对。如何在从wdsl创建的生成的类中执行他的外观?以下是与您的问题相关的SO问题的一行。 C# equivalent to php associative array一个答案说试试Dictionary<String, String>。也许值得尝试使用KeyValuePair<String, String>作为对象数组传递。

KeyValuePair<String, String> parm = new KeyValuePair<String, String>("apiKey","******"); 

var response = service.getTransactions(new object[] { parm }); 
+0

看看我写给他的“skumar”在他的评论... – 2013-04-25 20:07:00

+0

@MatanL,这是一个艰难的。我认为这将归结为手改变wdsl之前,你导入,以帮助它创建类型,将与此 A – 2013-04-25 20:29:18

2

要么你可以使用的键值对类或字典类,如下所示:

 Dictionary<string, string> d = new Dictionary<string, string>(); 
     d.Add("apiKey", "******"); 
     var response = new object[] { d }; 

     KeyValuePair<string, string> d = new KeyValuePair<string, string>("apiKey", "******"); 
     var response = new object[] { d }; 
+0

我已经尝试字典..我将检查keyvaluepair现在... – 2013-04-25 20:03:51

+0

{“类型系统。 Collections.Generic.KeyValuePair'2 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken =],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken =]]不是所期望的,使用XmlInclude或SoapInclude属性指定静态未知的类型。“} – 2013-04-25 20:05:55

+0

这是我得到的例外......提琴手didnt捕获的任何请求发送我们的我的电脑的所有.. – 2013-04-25 20:06:28

3

我挖得更深一些,到这个迷人的话题,不得不说,有什么相当于一个关联数组工作是只是.NET SOAP实现中的一个痛处。关联数组表示为IDictionary(例如Hashtable) - 但拒绝序列化(尝试它!)。

下面的代码是最接近我能得到 - 由于某种原因(?BUG)它不会在.NET Framework的工作,但确实在Mono的工作。从单

using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Schema; 
using System.Xml.Serialization; 
using System.Collections.Generic; 
using System.Text; 

public class Test 
{ 
    /* 
    * as per http://stackoverflow.com/a/1072815/2348103 
    */ 
    public class Item 
    { 
    [XmlElement(Form = XmlSchemaForm.Unqualified)] 
    public string key; 

    [XmlElement(Form = XmlSchemaForm.Unqualified)] 
    public string value; 
    } 

    [SoapType(TypeName = "Map", Namespace = "http://xml.apache.org/xml-soap")] 
    public class Map : List<Item> 
    { 
    } 

    public static void Main() 
    { 
    Map map = new Map(); 
    map.Add(new Item { key="foo", value="bar" }); 
    map.Add(new Item { key="quux", value="barf" }); 

    XmlTypeMapping mapping = 
     (new SoapReflectionImporter()).ImportTypeMapping(map.GetType()); 
    XmlSerializer serializer = new XmlSerializer(mapping); 
    XmlTextWriter writer = new XmlTextWriter(System.Console.Out); 
    writer.Formatting = Formatting.Indented; 
    writer.WriteStartElement("root"); 
    serializer.Serialize(writer, map); 
    writer.WriteEndElement(); 
    writer.Close(); 
    } 
} 

/* 
    // 
    // does not work - but see http://msdn.microsoft.com/en-us/magazine/cc164135.aspx 
    // 
    public class Map : IXmlSerializable 
    { 
    const string NS = "http://xml.apache.org/xml-soap"; 
    public IDictionary dictionary; 
    public Map() 
    { 
     dictionary = new Hashtable(); 
    } 

    public Map(IDictionary dictionary) 
    { 
     this.dictionary = dictionary; 
    } 

    public void WriteXml(XmlWriter w) 
    { 
     w.WriteStartElement("Map", NS); 
     foreach (object key in dictionary.Keys) 
     { 
     object value = dictionary[key]; 
     w.WriteStartElement("item", NS); 
     w.WriteElementString("key", NS, key.ToString()); 
     w.WriteElementString("value", NS, value.ToString()); 
     w.WriteEndElement(); 
     } 
     w.WriteEndElement(); 
    } 

    public void ReadXml(XmlReader r) 
    { 
     r.Read(); // move past container 
     r.ReadStartElement("dictionary"); 
     while (r.NodeType != XmlNodeType.EndElement) 
     { 
     r.ReadStartElement("item", NS); 
     string key = r.ReadElementString("key", NS); 
     string value = r.ReadElementString("value", NS); 
     r.ReadEndElement(); 
     r.MoveToContent(); 
     dictionary.Add(key, value); 
     } 
    } 
    public System.Xml.Schema.XmlSchema GetSchema() { return null; } 
    } 
*/ 

输出示例:

<q1:Map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" d2p1:arrayType="Item[2]" xmlns:d2p1="http://schemas.xmlsoap.org/soap/encoding/" xmlns:q1="http://xml.apache.org/xml-soap"> 
    <Item href="#id2" /> 
    <Item href="#id3" /> 
</q1:Map> 
[...] 

示例输出从.NET:

<q1:Array xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" q1:arrayType="Item[2]" xmlns:q1="http://schemas.xmlsoap.org/soap/encoding/"> 
    <Item href="#id2" /> 
    <Item href="#id3" /> 
</q1:Array> 
[...] 
+0

你的答案是迄今为止我见过的最好的答案,尽管它在.net上不起作用......如果人们可以理解为什么..我会给你所做努力的赏金。谢谢。 – 2013-05-05 13:12:58