Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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
Asp.net 使用HttpWebRequest向json web服务传递参数_Asp.net_Json_Httpwebrequest - Fatal编程技术网

Asp.net 使用HttpWebRequest向json web服务传递参数

Asp.net 使用HttpWebRequest向json web服务传递参数,asp.net,json,httpwebrequest,Asp.net,Json,Httpwebrequest,我在向webservice传递参数时遇到问题,除了JSON中的POST数据之外。我正在为此使用HttpWebRequest,下面是我迄今为止尝试过的代码,但everytime服务器返回这两个错误中的任何一个: 错误1: {"command":null,"handle":null,"code":2003,"msg":"Required parameter missing","error":["Parameter 'login_handle' missing.","Parameter 'login_

我在向webservice传递参数时遇到问题,除了
JSON
中的
POST
数据之外。我正在为此使用
HttpWebRequest
,下面是我迄今为止尝试过的代码,但everytime服务器返回这两个错误中的任何一个:

错误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":[]}
{"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":[]}]}
错误2:

{"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":[]}
{"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":[]}]}
{“command”:null,“handle”:null,“code”:2400,“msg”:“command failed”,“error”:[“Internal Server error.”导致以下错误:array_key_exists()[function.array key exists]:第二个参数应该是数组或对象“],“params”:[],“svTRID”:null,“response”:[],“children”:[{“command”:“Internal Server error.”,“handle”:null,“code”:2400,“msg”:“Command failed”,“error”:[“array\u key\u exists()[function.array key exists]:第二个参数应该是数组或对象],“params”:{“errno”:2,“errstr”:“array\u key\u exists()[function.array key exists]:第二个参数应该是数组或对象”,“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;
            }
试试看
{
ascienceoding encoding=新的ascienceoding();
字典数据=新字典();
数据[“登录句柄”]=“测试”;
数据[“登录\通过”]=“测试”;
System.Net.WebRequest webReq=System.Net.WebRequest.Create(url);
webReq.Method=“POST”;
webReq.ContentType=“application/json;charset=utf-8”;
DataContractJsonSerializer ser=新的DataContractJsonSerializer(data.GetType());
MemoryStream ms=新的MemoryStream();
ser.WriteObject(ms,数据);
String json=Encoding.UTF8.GetString(ms.ToArray());
StreamWriter writer=newstreamwriter(webReq.GetRequestStream());
Write.Write(json);
writer.Close();
System.Net.WebResponse webResp=webReq.GetResponse();
System.IO.StreamReader sr=新的System.IO.StreamReader(webResp.GetResponseStream());
字符串s=sr.ReadToEnd().Trim();
}
捕获(例外情况除外)
{
字符串e=例如消息;
}

如果我使用
string data=“[login_handle:'username',login_pass:'password']”
而不是
Dictionary
,我收到了错误号2。

没关系,我自己解决了它,而不是使用Dictionary类型,我使用了如下的匿名类型
var data=new{login\u handle=“test”,login\u pass=“test”}解决了我的问题。

您应该创建一个DataContract类(我们称之为JsonData),它有两个名为login\u handle和login\u pass的数据成员

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

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