微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin Forms - PostAsync 错误请求

如何解决Xamarin Forms - PostAsync 错误请求

我正在尝试进行身份验证调用,无论我尝试什么,我都会收到 400,错误请求。这是我的代码

        try
        {
            HttpClient myHttpClient = new HttpClient();
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            //client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type","application/x-www-form-urlencoded");

            LoginApiRequestModel loginRequestModel = new LoginApiRequestModel()
            {
                Email = email,Password = password,UserScope = "restapi",ClientSecret = Constants.ClientSecret,GrantType = Constants.GrantType,ClientId = "5a8f011c-xxxx-xxxx"
            };

            var serializedLogin = JsonConvert.SerializeObject(loginRequestModel);
            var content = new StringContent(serializedLogin,Encoding.UTF8,"application/json");

            var response = await myHttpClient.PostAsync("https://xxxxxx/oauth/token",content);

            if (response.IsSuccessstatusCode)
            {
                using (var stream = await response.Content.ReadAsstreamAsync())
                using (var reader = new StreamReader(stream))
                using (var json = new JsonTextReader(reader))
                {
                    var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);

                    await SecureStorage.SetAsync("accesstoken",jsoncontent.Accesstoken);
                    await SecureStorage.SetAsync("refreshToken",jsoncontent.RefreshToken);
                    await SecureStorage.SetAsync("email",email);

                    return jsoncontent;
                }
            }
            else
            {
                var contents = response.Content.ReadAsstringAsync().Result;
                Debug.WriteLine(contents);

                return null;
            }
        }
        catch (Exception ex)
        {
            Debug.Write(ex);
            return null;
        }

我尝试过使用 SendAsync、所有可能的标头和编码类型,但没有任何效果

我什至尝试过使用 Xcode、本机 iOS 进行调用,并且可以正常工作。这是我在 iOS 中的代码

var headers = NetworkClient.defaultHeaders()
headers.add(name: "Content-Type",value: "application/x-www-form-urlencoded")
apiRequest(.authentication,parameters: params,encoding: URLEncoding.httpBody,headers: headers) { (result) in ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。