Click here to Skip to main content
Sign Up to vote bad
good
See more: C#HTTP
Hi All,
 
I have to post some data from asp.net 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;
        try
        {
            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;
        }
        finally
        {
            objRequest = null;
            objEncoding = null;
            reqStream = null;
            if (objResponse != null)
                objResponse.Close();
            objResponse = null;
            objReader = null;
        }
        return strReturn;
    }
Thanks in advance.
 
Priyanka
Posted 20 Dec '11 - 23:28
Edited 20 Dec '11 - 23:48


1 solution

Hi,
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 333
1 OriginalGriff 261
2 Mohammed Hameed 198
3 Mayur_Panchal 153
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid