首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >x-www-form-urlencoded,Xamarin

x-www-form-urlencoded,Xamarin
EN

Stack Overflow用户
提问于 2018-07-26 14:44:45
回答 1查看 1.7K关注 0票数 1

我正在开发Xamarin.Android应用程序。我必须使用内容类型为x-www-form-urlencoded的rest API。我无法成功地调用Rest API,在此之前,我使用了许多web服务,但我是第一次使用这种类型的API。我被困在这里了。我尝试了两种方式来使用它:

代码语言:javascript
复制
 public static string makePostEncodedRequest(string url, string jsonparams)
    {
        string ret = "";
        var httpwebrequest = (HttpWebRequest)WebRequest.Create(url);
        httpwebrequest.ContentType = "application/x-www-form-urlencoded";
        //httpwebrequest.Accept = Config.JsonHeaderAJ;
        httpwebrequest.Method = Methods.Post.ToString();

        byte[] bytearray = Encoding.UTF8.GetBytes(jsonparams);

        using (var streamWriter = new StreamWriter(httpwebrequest.GetRequestStream(), Encoding.ASCII))
        {
            streamWriter.Write(bytearray);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpwebrequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            ret = streamReader.ReadToEnd();
        }
        return ret;
    }       

下一个是:

代码语言:javascript
复制
   Dictionary<string, string> requestParams = new Dictionary<string, string ();

        requestParams.Add("value=", data1);
        requestParams.Add("&value=", data2);

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

            HttpResponseMessage response = client.PostAsync(Config.DomainURl + Config.StudentLoginUrl, new FormUrlEncodedContent(requestParams)).Result;

            var tokne = response.Content.ReadAsStringAsync().Result;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 17:38:21

我在我的项目中使用了以下代码来验证用户,这可能会对你有所帮助。

代码语言:javascript
复制
public static async Task<UserData> GetUserAuth(UserAuth userauth)
    {
        bool asd= CheckNetWorkStatus().Result;
        if (asd)
        {
            var client = new HttpClient(new NativeMessageHandler());
            client.BaseAddress = new Uri(UrlAdd);// ("http://192.168.101.119:8475/");
            var postData = new List<KeyValuePair<string, string>>();
            var dto = new UserAuth { grant_type = userauth.grant_type, password = userauth.password, username = userauth.username };
            var nvc = new List<KeyValuePair<string, string>>();
            nvc.Add(new KeyValuePair<string, string>("grant_type", userauth.grant_type));
            nvc.Add(new KeyValuePair<string, string>("password", userauth.password));
            nvc.Add(new KeyValuePair<string, string>("username", userauth.username));
            var req = new HttpRequestMessage(HttpMethod.Post, UrlAdd + "token") { Content = new FormUrlEncodedContent(nvc) };

            var res = await client.SendAsync(req);
            if (res.IsSuccessStatusCode)
            {
                string result = await res.Content.ReadAsStringAsync();
                var userData = JsonConvert.DeserializeObject<UserData>(result);
                userData.ErrorMessage = "true";
                return userData;
            }
            else
            {
                UserData ud = new UserData();
                ud.ErrorMessage = "Incorrect Password";
                return ud;
            }
        }
        else
        {
            UserData ud = new UserData();
            ud.ErrorMessage = "Check Ur Connectivity";
            return ud;
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51532553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档