Click here to Skip to main content
11,583,865 members (45,437 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# HTTP
Hi All,

I have to post some data from to Webservice using HTTP post.

While I am trying to POST data to a Restful service and getting this error. Any help greatly appreciated.
Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException'
Position = 'dataStream.Position' threw an exception of type 'System.NotSupportedException'
Below is my code.
protected string GetResponseWithPost(string StrURL, string strPostData)
        string strReturn = "";
        HttpWebRequest objRequest = null;
        ASCIIEncoding objEncoding = new ASCIIEncoding();
        Stream reqStream = null;
        HttpWebResponse objResponse = null;
        StreamReader objReader = null;
            objRequest = (HttpWebRequest)WebRequest.Create(StrURL);
            objRequest.Method = "POST";
            byte[] objBytes = objEncoding.GetBytes(strPostData);
            objRequest.ContentLength = objBytes.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";
            reqStream = objRequest.GetRequestStream();
            reqStream.Write(objBytes, 0, objBytes.Length);
            IAsyncResult ar = objRequest.BeginGetResponse(new AsyncCallback(GetScrapingResponse), objRequest);
            //// Wait for request to complete
            ar.AsyncWaitHandle.WaitOne(1000 * 60 * 3, true);
            if (objRequest.HaveResponse == false)
                throw new Exception("No Response!!!");
            objResponse = (HttpWebResponse)objRequest.EndGetResponse(ar);
            objReader = new StreamReader(objResponse.GetResponseStream());
            strReturn = objReader.ReadToEnd();
        catch (Exception exp)
            throw exp;
            objRequest = null;
            objEncoding = null;
            reqStream = null;
            if (objResponse != null)
            objResponse = null;
            objReader = null;
        return strReturn;
Thanks in advance.

Posted 20-Dec-11 23:28pm
Edited 20-Dec-11 23:48pm

1 solution

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

Solution 1

It would help if you could identify on what line you got the error but looking at the code it seem that you are trying to handle a async call in a synchrounous way.

You should call the EndGetResponce from the callback and as such make sure that the data buffer can be accessed (safely) from both methods.

Hope this helps.

Cheers, AT

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

  Print Answers RSS
0 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 314
2 virusstorm 268
3 Richard Deeming 220
4 Dave Kreskowiak 200
0 OriginalGriff 1,623
1 Sergey Alexandrovich Kryukov 1,135
2 Abhinav S 701
3 Dave Kreskowiak 627
4 Suvendu Shekhar Giri 568

Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2015
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