使用HttpWebRequest将参数传递给json Web服务

时间:2011-01-27 09:56:51

标签: asp.net json httpwebrequest

我在将参数传递给webservice时遇到问题,POST中的JSON数据除外。我正在使用HttpWebRequest,以下是我到目前为止尝试过的代码,但每次服务器都会返回以下两个错误中的任何一个:

错误1:

{"command":null,"handle":null,"code":2003,"msg":"Required parameter missing","error":["Parameter 'login_handle' missing.","Parameter 'login_pass' missing."],"params":{"0":{"Key":"login_handle","Value":"test"},"1":{"Key":"login_pass","Value":"test"},"handle":"example.com"},"svTRID":null,"response":[]}

错误2:

{"command":null,"handle":null,"code":2400,"msg":"Command failed","error":["Internal Server Error. resulted in the following error: array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists<\/a>]: The second argument should be either an array or an object"],"params":[],"svTRID":null,"response":[],"children":[{"command":"Internal Server Error.","handle":null,"code":2400,"msg":"Command failed","error":["array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists<\/a>]: The second argument should be either an array or an object"],"params":{"errno":2,"errstr":"array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists<\/a>]: The second argument should be either an array or an object","errfile":"\/home\/ote\/httpapi\/v1\/index.php","errline":54},"svTRID":null,"response":[]}]}

以下是代码:

try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();


                Dictionary<string, string> data = new Dictionary<string, string>();
                data["login_handle"] = "test";
                data["login_pass"] = "test";               


                System.Net.WebRequest webReq = System.Net.WebRequest.Create(url);
                webReq.Method = "POST";
                webReq.ContentType = "application/json; charset=utf-8";
                DataContractJsonSerializer ser = new DataContractJsonSerializer(data.GetType());
                MemoryStream ms = new MemoryStream();
                ser.WriteObject(ms, data);
                String json = Encoding.UTF8.GetString(ms.ToArray());
                StreamWriter writer = new StreamWriter(webReq.GetRequestStream()); 
                writer.Write(json); 
                writer.Close();

                System.Net.WebResponse webResp = webReq.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(webResp.GetResponseStream());
                string s = sr.ReadToEnd().Trim();
            }
            catch (Exception ex)
            {
                string e = ex.Message;
            }

如果我使用string data = "[login_handle:'username',login_pass:'password']";代替Dictionary<string, string>,我会收到错误编号2.

2 个答案:

答案 0 :(得分:2)

没关系,我自己解决了,而不是使用字典类型我使用匿名类型,如var data = new { login_handle = "test", login_pass = "test" };,它解决了我的问题。

答案 1 :(得分:0)

你应该做的是创建一个DataContract类(我们将调用JsonData),它有两个名为login_handle和login_pass的DataMembers。

然后,在DataContractJsonSerializer中,将typeof(JsonData)传递给构造函数。

此解决方案是最好的,因为您无法使用匿名类型创建复杂类型。此外,通过为您的DataContracts提供父母,您可以轻松创建复杂的JSON。