简体   繁体   English

在C#中调用api函数

[英]Calling api function in c#

I'm trying to make a call to a url to get a json object returned. 我正在尝试调用url以返回json对象。

I have an example in coldfusion i am trying to use to base it off of. 我有一个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!

I'm trying to convert this to c# and heres what I have. 我正在尝试将其转换为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();
            }
        }
    }
}

I'm not getting the expected result back, which is just "Hello World" 我没有得到预期的结果,只是“ Hello World”

Simple way to call it is to add a Service Reference. 调用它的简单方法是添加服务引用。

In the Solution Explorer right click References then add Service Reference. 在解决方案资源管理器中,右键单击“ 引用”,然后添加“服务引用”。

高级

On the Add service Reference dialog Click on Advanced 在“添加服务引用”对话框上,单击“高级”。 在此处输入图片说明

Then click on Add Web Reference 然后单击添加Web参考 在此处输入图片说明

Enter the URL adding the WSDL: " https://secure.test.com/webservices/ws_users.cfc?wsdl " 输入添加WSDL的URL:“ https://secure.test.com/webservices/ws_users.cfc?wsdl

在此处输入图片说明

Then Click Add Reference 然后单击添加参考 在此处输入图片说明

Finally in your code you can call the method like this: 最后,您可以在代码中调用如下方法:

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

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

Right now is returning : Fail|Could Not Validate Authenticity i guess you have to pass it real credentials in order to authenticate. 现在返回: Fail |无法验证真实性我想您必须传递真实的凭据才能进行验证。

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM