2013-03-07 56 views
0

在我的情况我的API处理JSON阅读它作为邮政JSON数据作为的WebRequest的查询字符串

if (context.Request.Files.Count > 0) 
     { 
      jsonData = context.Request.Params["key"]; 
     } 

从我的应用程序如何可以发送一个网络请求,这个API没有JSON作为查询字符串参数。正弦Json是冗长的,我知道查询字符串是有限的。

对于android的ios这个api工作正常。

我试图将它添加到标题。但徒劳无益。

我该如何添加它,以便“jsonData = context.Request.Params [”key“];”将得到我的json。 我的请求格式在这里。

  urlS="http://abc.ashx?Key="+jsonRequestS; 
      string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); 
      var webRequest1 = (HttpWebRequest)WebRequest.Create(urlS); 
      webRequest1.Method = "POST"; 
      webRequest1.KeepAlive = false; 
      webRequest1.ContentType =string.Format("multipart/form-data; boundary={0}", boundary);//     
      Stream postDataStream1 = handler.GetPostStream(boundary,jsonRequestS);    // Writes boundary and files to memory stream. 

      webRequest1.ContentLength = postDataStream1.Length; 
      Stream reqStream1 = webRequest1.GetRequestStream(); 

      postDataStream1.Position = 0; 

      var bufferBytes = new byte[postDataStream1.Length]; 
      postDataStream1.Read(bufferBytes, 0, bufferBytes.Length); 
      reqStream1.Write(bufferBytes, 0, bufferBytes.Length); 

      postDataStream1.Close(); 
      reqStream1.Close(); 

      var sReader = new StreamReader(webRequest1.GetResponse().GetResponseStream());//here Am getting the error. 
      string resultS = sReader.ReadToEnd(); 

在此先感谢。

+0

你能提供一个例子,声明要求你会喜欢用?你说你不想把json放在查询字符串中,那么你说你想从查询字符串中读取json。 – 2013-03-07 14:19:32

+0

“jsonData = context.Request.Params [”key“];这是声明在那里读取我的Json数据..我想知道如何发送json到API,以便API可以读取它。但是作为Qry字符串,我可以发送一个冗长的Json,所以我应该使用哪种方法。 – 2013-03-08 04:08:07

回答

0

感谢您的回复。 我找到了解决这个问题的方法。

写的Json对标题的请求流为

postDataStream.Write(boundarybytes, 0, boundarybytes.Length); 
      string header = string.Format("Content-Disposition: form-data; name=\"Key\"\r\n\r\n"); 
      headerbytes = Encoding.Default.GetBytes(header); 
      postDataStream.Write(headerbytes, 0, headerbytes.Length); 
      headerbytes = Encoding.Default.GetBytes(jsonRequestS);//writing json request. 
      postDataStream.Write(headerbytes, 0, headerbytes.Length); 

,并在服务器端将是精细读中

jsonData = context.Request.Params["key"];