将Json Data作为webrequest的查询字符串发布 [英] Post Json Data as Query String of a webrequest

查看:75
本文介绍了将Json Data作为webrequest的查询字符串发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的场景中,我的API处理json读取它为



if(context.Request.Files.Count> 0)

{

jsonData = context.Request.Params [key];

}


来自我的应用程序的
如何在不使用json作为查询字符串参数的情况下向此api发送Web请求。 Sine Json很长,我知道查询字符串是有限的。



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



i试图将它添加到标题中。但是徒劳无功。



如何添加它以便jsonData = context.Request.Params [key];将得到我的json。

我的请求格式在这里。

 urlS =   HTTP://abc.ashx密钥=? + 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); // 将边界和文件写入内存流。

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

postDataStream1.Position = 0 ;

var bufferBytes = new 字节 [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( )); // 这里得到错误。
string resultS = sReader.ReadToEnd();





提前致谢。

解决方案

Hello Ashish ,



查看您的客户端代码片段首先需要做的是将第一行更改为如下所示

 urlS =   http://abc.ashx; 



下一步是更改创建 postDataStream1 的行。在这一行,你传递边界& jsonRequestS GetPostStream 。假设 jsonRequestS 是一个字符串,您需要做的就是将该行更改为

 handler.GetPostStream(boundary,  key = + jsonRequestS); 



记住发送的POST数据key = value格式。由于您要传输任何文件,因此不需要ContentTypeasmultipart / form-data; boundary = ------------------------- 334234ds。 ContentType为application / x-www-form-urlencoded就足够了。



问候,


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



将Json写入请求流,标题为



postDataStream.Write(boundarybytes,0,boundarybytes.Length);

string header = string.Format(Content-Disposition:form-data; name = \Key \\ rr \ n \\ n;

headerbytes = Encoding.Default.GetBytes(header);

postDataStream.Write(headerbytes,0,headerbytes.Length );

headerbytes = Encoding.Default.GetBytes(jsonRequestS); //写json请求。

postDataStream.Write(headerbytes,0,headerbytes.Length);

并且在服务器端它将在声明中被很好地阅读



jsonData = context.Request.Params [key];

In my scenario my API handles the json reading it as

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

from my application how can i send a web request to this api without json as query string parameter. Sine Json is lengthy and i know query string is limited.

For android an ios this api works fine.

i have tried to add it to the header. but in vain.

how can i add it so that "jsonData = context.Request.Params["key"]; " will get my json.
my request format is here.

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();



Thanks in advance.

解决方案

Hello Ashish,

Looking at your client code snippet first thing you need to do is to change very first line to read as follows

urlS="http://abc.ashx";


The next thing is to change the line which creates postDataStream1. On this line you are passing boundry & jsonRequestS to GetPostStream. Assuming that jsonRequestS is a string all you need to do is change that line to

handler.GetPostStream(boundary, "key=" + jsonRequestS);


Remember POST data is sent in "key=value" format. Since you are transferring any file(s), ContentTypeas "multipart/form-data; boundary=-------------------------334234ds" is unnecessary. ContentType as "application/x-www-form-urlencoded" should be sufficient.

regards,


Thanks for ur Reply. I found a way to solve this.

Write the Json to the request stream with the header as

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);
and at the server side it will be fine read in the statement

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


这篇关于将Json Data作为webrequest的查询字符串发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆