-1

I'm trying to send large json string in my c# application to an asp.net page using querystring (POST method), but because the string is too long it give me this msg: Invalid uri: the uri link is too long.

Is there is another solution for my problem !?

if(allRecords.Count > 0)
    for (int j = 0; j < allRecords.Count; j++)
    {
        queryString += JsonConvert.SerializeObject(allRecords[j], Newtonsoft.Json.Formatting.Indented);
    }
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(queryString);

// Set up Request.
HttpWebRequest webReq = WebRequest.Create(onlineApp) as HttpWebRequest;
webReq.ContentType = "text/plain";
webReq.Method = "POST";
webReq.Credentials = CredentialCache.DefaultCredentials;
webReq.ContentLength = data.Length;

// Send Request.


Stream newStream = webReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

// get Response.
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

string tt = reader.ReadToEnd();

reader.Close();
response.Close();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Belal_G
  • 75
  • 1
  • 2
  • 10
  • 1
    POST data is not part of the URI, so the error doesn't make much sense to me. What does the ASP.NET page do with the POST data? Could the error be there? – svick Jul 20 '14 at 10:15
  • How about using the `WebClient` class and one of the [`WebClient.UploadData` overloads](http://msdn.microsoft.com/en-us/library/tdbbwh0a(v=vs.110).aspx) instead of the `WebRequest` class? – Uwe Keim Jul 20 '14 at 11:27
  • I might be missing it, but based only on the above, what is `querystring`? The variable name? The actual `query string` (of uri)? I see the former (which you seem to be POSTing)..but not the latter... – EdSF Jul 20 '14 at 16:02
  • Svick..the error occure in c# page .. before i can send it ! and my querystring is too long .. i check it. i need to send the json string to asp.net page ! – Belal_G Jul 21 '14 at 05:25
  • I dont see where you are creating you URL. Is the URL the onlineApp variable? There would be no need to create a URL with a querystring if you are POSTing the data. e.g. http://www.com?s=querystring. If the API is expecting a POST, it will bring in the data you are writing to the request stream. – Keith Aymar Feb 07 '20 at 20:15
  • Also depending on the API the content type may need to be "application/json" – Keith Aymar Feb 07 '20 at 20:18

1 Answers1

0

I think your problem in your WebRequest , when you create new Webrequest , check you webrequest string.

nick shp
  • 90
  • 1
  • 9