Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# x-www-form-url编码为Xamarin_C#_Web Services_Post_Xamarin.android - Fatal编程技术网

C# x-www-form-url编码为Xamarin

C# x-www-form-url编码为Xamarin,c#,web-services,post,xamarin.android,C#,Web Services,Post,Xamarin.android,我正在开发Xamarin.Android应用程序。我必须使用内容类型为x-www-form-urlencoded的RESTAPI。我无法成功调用RESTAPI,在此之前,我使用了许多web服务,但我第一次使用这种类型的API。我被困在这里面了。 我尝试了两种方法来消费它: public static string makePostEncodedRequest(string url, string jsonparams) { string ret = "";

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

 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;
    }       
下一个问题是:

   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;
        }

Dictionary requestParams=new Dictionary我已经使用了以下代码来验证我项目中的用户,这可能会对您有所帮助

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;
        }
    }
公共静态异步任务GetUserAuth(UserAuth UserAuth) { bool asd=CheckNetWorkStatus()。结果; 如果(asd) { var client=newhttpclient(new NativeMessageHandler()); client.BaseAddress=新Uri(UrlAdd);/(“http://192.168.101.119:8475/"); var postData=新列表(); var dto=newuserauth{grant_type=UserAuth.grant_type,password=UserAuth.password,username=UserAuth.username}; var nvc=新列表(); 添加(新的KeyValuePair(“grant_type”,userauth.grant_type)); 添加(新的KeyValuePair(“密码”,userauth.password)); 添加(新的KeyValuePair(“用户名”,userauth.username)); var req=new-HttpRequestMessage(HttpMethod.Post,UrlAdd+“token”){Content=new-FormUrlEncodedContent(nvc)}; var res=wait client.SendAsync(req); 如果(res.IsSuccessStatusCode) { 字符串结果=wait res.Content.ReadAsStringAsync(); var userData=JsonConvert.DeserializeObject(结果); userData.ErrorMessage=“true”; 返回用户数据; } 其他的 { UserData ud=新的UserData(); ud.ErrorMessage=“密码不正确”; 返回ud; } } 其他的 { UserData ud=新的UserData(); ud.ErrorMessage=“检查您的连接”; 返回ud; } }
jsonparams
可能是一个误导性的参数名称,也可能正是问题所在。感谢您的帮助,请告诉我有关NativeMessageHandler的信息。它可以从哪个dll访问used@MohammedDastagir使用了modernhttpclient.2.4。2@MohammedDastagir很高兴听你这么说,快乐