wp7 - 使用RestSharp在请求正文中使用参数POST http请求

时间:2012-09-19 09:28:55

标签: windows-phone-7 restsharp

我正在尝试通过请求将参数POST到返回JSON对象的服务。该服务适用于Android和iOS。我试图让这个为wp7工作。该服务要求内容类型为'application / json'我已粘贴下面设置http请求的代码:

            var client = new RestClient(baseurl);
            var request = new RestRequest();
            request.Resource = "login";
            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.AddHeader("content-type", "application/json");         
            request.RequestFormat = DataFormat.Json;

            var postData = new Dictionary<string, string>()
            {
                {"key1",value1},
                {"key2",value2}
            };

            request.AddBody(postData); 
            client.ExecuteAsync(request, response =>
            {
                var jsonUser = response.Content;
            });

我从服务器获得的响应错误是内部服务器错误。上面的代码有什么问题。我也尝试过request.AddParameter方法,但结果却相同。代码如下:

            var client = new RestClient(baseurl);
            var request = new RestRequest();
            request.Resource = "login";
            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.AddHeader("content-type", "application/json");         
            request.RequestFormat = DataFormat.Json;

            var postData = new Dictionary<string, string>()
            {
                {"key1",value1},
                {"key2",value2}
            };
            var json = JsonConvert.SerializeObject(postData);
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            client.ExecuteAsync(request, response =>
            {
                var jsonUser = response.Content;
            });

在任何一种情况下,我有什么问题吗?

0 个答案:

没有答案