Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查API是否有效_C#_Xamarin - Fatal编程技术网

C# 检查API是否有效

C# 检查API是否有效,c#,xamarin,C#,Xamarin,我在Xamarin工作,目前正在从API中获取一些数据,但我不确定如何检查我是否真正完成了登录过程。我在登录过程中使用了以下代码: public async Task<Token> Login(){ var postData = new List<KeyValuePair<string, string>>(); postData.Add(new KeyValuePair<string, string>("grant_type",

我在Xamarin工作,目前正在从API中获取一些数据,但我不确定如何检查我是否真正完成了登录过程。我在登录过程中使用了以下代码:

public async Task<Token> Login(){
    var postData = new List<KeyValuePair<string, string>>();
    postData.Add(new KeyValuePair<string, string>("grant_type", 
    "password"));
    postData.Add(new KeyValuePair<string, string>("username", 
     "********"));
    postData.Add(new KeyValuePair<string, string>("password", 
    "********"));
    var content = new FormUrlEncodedContent(postData);
    var weburl = "**********************";
    var response = await PostResponse<Token>(weburl, content);
    DateTime dt = new DateTime();
    dt = DateTime.Today;
    response.Expire_Date = dt.AddSeconds(response.Expires_In);
    return response;
}
当我写出a时,我得到的只是一个System.Threading.Tasks.Task[Exploration.ExplorationObject]错误,没有太多的错误

API应该给出一个errormsg响应和一个长的数字和字符字符串

我想真正的问题是:如何从API中获取所需的两条消息

PS.RestService的构造函数:

public RestService()
    {
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded' "));
    }
我更新了解决方案来解决异步问题,现在我得到了一个异常

我这样调用异步方法:

public partial class DayView : ContentPage
{
    public DayView ()
    {
        InitializeComponent();
        getData();
    }

    public async void getData()
    {
        var result = await App.RestService.Login();
        string a = result.ToString();
        BindingContext = new Response(a);
    }
}

该应用程序现在可以在我的iphone上运行,但我收到了CertificateUnkownTlsException,这是否意味着我已经开始与我的API进行第一次接触?

您没有得到有意义的响应,因为登录是一项异步任务,您将其视为同步调用,而实际上您应该等待它:

var result = await App.RestService.Login();

您需要等待登录呼叫。。。var result=wait App.RestService.Login;我想你比我的回答快了几秒钟。这是有道理的。。但是该方法告诉我该方法不是异步的,'请考虑使用'async'修饰符标记该方法,并将其返回类型更改为'Task'…我在其中使用ToString方法的方法也必须是异步的吗?如果编译器告诉您,App.RestService.Login不是异步的,那么这不是您上面向我们展示的登录方法,因为它被清楚地标记为异步。不要执行异步void。使用async Task来代替。是的,我认为他们使用了我调用toString方法的方法,因为我的登录方法确实是异步的。。
var result = await App.RestService.Login();