2017-10-05 54 views
0

我想調用一個url來得到一個json對象返回。在c調用api函數#

我在coldfusion中有一個例子,我試圖用它來基於它。

Test For IPCorg123: (the return should: Hello World)<br> 
<cfinvoke method="test20130401" returnvariable="rawReturn" 
webservice="https://secure.test.com/webservices/ws_users.cfc?wsdl"> 
    <cfinvokeargument name="accountlogincode" value="1"/> 
    <cfinvokeargument name="accountxmlcode" value="1"/> 
    <cfinvokeargument name="accountidspecialcode" value="1"/> 
    <cfinvokeargument name="authorlogin" value="1"/> 
    <cfinvokeargument name="authorpassword" value="1"/> 
    <cfinvokeargument name="TestString" value="Hello World/> 
</cfinvoke> 
<cfdump var="#rawReturn#"><br><br><br><br> 
Done! 

我試圖將其轉換爲C#和繼承人我有什麼。

class Program 
{ 
    public static string accountlogincodename = "accountlogincodevalue"; 
    public static string accountxmlcodename = "accountxmlcodevalue"; 
    public static string accountidspecialcodename = "accountidspecialcode"; 
    public static string authorloginname = "authorlogin"; 
    public static string authorpasswordname = "authorpassword"; 
    public static string TestStringname = "TestString"; 

    public static string accountlogincodevalue = "1"; 
    public static string accountxmlcodevalue = "1"; 
    public static string accountidspecialcodevalue = "1"; 
    public static string authorloginvalue = "1"; 
    public static string authorpasswordvalue = "1"; 
    public static string TestStringvalue = "Hello"; 

    static void Main() 
    { 
     RunAsync().Wait(); 
    } 

    static async Task RunAsync() 
    { 
     using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri("https://secure.test.com/webservices/ws_users.cfc?wsdl"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      HttpRequestMessage m = new HttpRequestMessage(); 
      m.Properties.Add(accountlogincodename, accountlogincodevalue); 
      m.Properties.Add(accountxmlcodename, accountxmlcodevalue); 
      m.Properties.Add(accountidspecialcodename, accountidspecialcodevalue); 
      m.Properties.Add(authorloginname, authorloginvalue); 
      m.Properties.Add(authorpasswordname, authorpasswordvalue); 
      m.Properties.Add(TestStringname, TestStringvalue); 

      // New code: 
      HttpResponseMessage response = await client.SendAsync(m); 
      if (response.IsSuccessStatusCode) 
      { 
       var x = await response.Content.ReadAsStringAsync(); 
      } 
     } 
    } 
} 

我沒有得到預期的結果返回,這僅僅是「Hello World」的

+1

您需要告訴我們Web服務端點('https://secure.test.com/webservices/ws_users.cfc?wsdl')實際上期望的是什麼。 ColdFusion使用WSDL方案構建請求,而不包含在您的帖子中。 – Alex

+0

它在冷聚變部分。 xsdl鏈接位於webservice參數的冷融合部分。你在談論不同的部分? – DidIReallyWriteThat

+0

Duh,我沒想到'secure.test.com'是真正的域名。 – Alex

回答

2

簡單的方法來調用它是添加一個服務引用。

在解決方案資源管理器中,右鍵單擊參考然後添加服務引用。

Advanced

在添加服務引用對話框中單擊高級 enter image description here

然後單擊添加Web引用 enter image description here

輸入URL添加WSDL: 「https://secure.test.com/webservices/ws_users.cfc?wsdl

enter image description here

然後點擊添加引用 enter image description here

最後,在你的代碼,你可以這樣調用方法:

com.test.secure.Ws_usersService ws = new com.test.secure.Ws_usersService(); 

var result = ws.test20130401(accountlogincodevalue, accountxmlcodevalue, accountidspecialcodevalue, authorloginvalue, authorpasswordvalue, TestStringvalue); 

眼下正在恢復:失敗|無法驗證真僞我猜你必須傳遞真實的憑據才能進行身份驗證。

希望得到這個幫助!