Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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.
Posted
Updated 7-Mar-13 17:20pm
v2

Hello Ashish,

Looking at your client code snippet first thing you need to do is to change very first line to read as follows
C#
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
C#
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,
 
Share this answer
 
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"];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900