2017-10-05 57 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); 

眼下正在恢复:失败|无法验证真伪我猜你必须传递真实的凭据才能进行身份验证。

希望得到这个帮助!