Click here to Skip to main content
13,045,657 members (52,217 online)
Rate this:
Please Sign up or sign in to vote.
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.
                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);
                var sReader = new StreamReader(webRequest1.GetResponse().GetResponseStream());//here Am getting the error.
                string resultS = sReader.ReadToEnd();

Thanks in advance.
Posted 6-Mar-13 21:05pm
Updated 7-Mar-13 17:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hello Ashish,

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

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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"];

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100