在c#中调用api函数

时间:2017-10-05 20:51:05

标签: c# web-services coldfusion

我正在尝试调用url来返回一个json对象。

我在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 个答案:

答案 0 :(得分:2)

调用它的简单方法是添加服务引用。

在解决方案资源管理器中,右键单击引用,然后添加服务引用。

<强> Advanced

在“添加服务引用”对话框中单击“高级” enter image description here

然后单击“添加Web引用” enter image description here

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

enter image description here

然后点击添加参考 enter image description here

最后在您的代码中,您可以调用这样的方法:

systemTime += 180000 //(180000 miliseconds is equivalent to 30 minutes)

现在正在返回:失败|无法验证真实性我猜您必须通过真实凭据才能进行身份验证。

希望这有帮助!