使用getstringasync()函数调用Azure API [英] Azure API calling with getstringasync() function

查看:97
本文介绍了使用getstringasync()函数调用Azure API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图点击一个托管在azure中的API。它在邮递员应用程序中完美运行。但它在从C#代码调用时返回null响应。它必须返回一个字符串,它是GET方法API。所以根据我的建议我使用getstringasync()函数
来命中API。它返回null响应。如果有人分享参考,将有助于我更多。在此先感谢。

I'm trying to hit a API which is hosted in azure. It works perfectly in postman app. But it returns null response while calling from C# code. It have to return a string and it is GET method API. So as per the advice I'm using getstringasync() function to hit the API. It's returns null response. If anyone share reference regarding that will help me more. Thanks in advance.

推荐答案

您好MJoseph您能否共享代码段。正如你已经提到过的那样,它是从Postman开始的。但只是为了排除事情,将GetStringAsync调用放在try catch块中,看看在调用c#代码时是否会出现异常。

Hi MJoseph Can you share the code snippet. As you have already mentioned it is working from Postman. But just to rule out things, put GetStringAsync call in a try catch block and see if some exception is coming while calling from c# code.

HttpClient client = new HttpClient(handler);

试试
{

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;任务<串GT; getStringTask = client.GetStringAsync("");
$


}

catch(exception ex)

{zh
return" error";
}

HttpClient client = new HttpClient(handler);
try
{
            Task<string> getStringTask = client.GetStringAsync("");

}
catch (Exception ex)
{
return "error";
}

还有一些推荐指南。您应该尽可能返回任务,因为无法等待返回void的异步方法。这种方法
的任何调用者必须能够继续完成而无需等待被调用的异步方法完成,并且调用者必须独立于异步方法生成的任何值或异常。

Also some recommended guidelines with Asynchronous programming. You should return a Task wherever possible, because a void-returning async method can't be awaited. Any caller of such a method must be able to continue to completion without waiting for the called async method to finish, and the caller must be independent of any values or exceptions that the async method generates.


这篇关于使用getstringasync()函数调用Azure API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆