HTTP POST Vs GET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cethie
    New Member
    • Jan 2010
    • 6

    HTTP POST Vs GET

    In the code below, DoGet is working very stable. But DoPost throws an uncatcheable InvalidOperatio nException randomly. I am lost. Any pointers will be of immense help.

    Code:
    /*
    	Environment
    	-------------
    	* NET CF 2.0
    	* WM 5.0(USA Mobile Pocket PC Emulator)
    	* Windows XP Professional SP2
    	* VS 2008
    */
    
    /*
    	The exception
    	------------------
       at System.Net.HttpWebRequest.set_ContentLength(Int64 value)
       at System.Net.HttpWebRequest.BufferConnectStream.WritingSucceeds()
       at System.Net.HttpWriteStream.doClose()
       at System.Net.HttpWriteStream.Finalize()
    */
    
    public static string DoPost(string url)
    {
    	// initialize from variables
    	string responseString = string.Empty;
    	ASCIIEncoding encoding = new ASCIIEncoding();
    	HttpWebResponse response;
    	byte[] data = encoding.GetBytes("dummy");
    	StreamReader reader;
    	HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    
    	//do the processing
    	SetRequestProperties(request, "POST"); // SETTING METHOD TO POST HERE
    	request.GetRequestStream().Write(data, 0, data.Length);
    	request.GetRequestStream().Close();
    	response = (HttpWebResponse)request.GetResponse();
    	reader = new StreamReader(response.GetResponseStream());
    	responseString = reader.ReadToEnd();
    
    	//clean up
    	response.Close();
    	response.GetResponseStream().Close();
    	response.GetResponseStream().Dispose();
    	reader.Close();
    	reader.Dispose();
    	reader = null;
    	response = null;
    	request = null;
    	encoding = null;
    
    	//return
    	MessageBox.Show("POST SUCCESS");
    	return responseString;
    
    }	
    
    public static string DoGet(string url)
    {
    	// initialize from variables
    	string responseString = string.Empty;
    	ASCIIEncoding encoding = new ASCIIEncoding();
    	HttpWebResponse response;
    	byte[] data = encoding.GetBytes("dummy");
    	StreamReader reader;
    	HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    
    	//do the processing
    	SetRequestProperties(request, "GET");  // SETTING METHOD TO GET HERE
    	response = (HttpWebResponse)request.GetResponse();
    	reader = new StreamReader(response.GetResponseStream());
    	responseString = reader.ReadToEnd();
    
    	//clean up
    	response.Close();
    	response.GetResponseStream().Close();
    	response.GetResponseStream().Dispose();
    	reader.Close();
    	reader.Dispose();
    	reader = null;
    	response = null;
    	request = null;
    	encoding = null;
    
    	//return
    	MessageBox.Show("GET SUCCESS"); 
    	return responseString;
    
    }
    
    private static void SetRequestProperties(HttpWebRequest request, string s)
    {
    	request.Method = s;
    	request.AllowWriteStreamBuffering = true;
    	request.KeepAlive = false;
    	request.ContentType = "application/x-www-form-urlencoded";
    	request.SendChunked = false;
    	request.Credentials = CredentialCache.DefaultCredentials;
    	request.UserAgent = "my mobile user agent";
    	request.Timeout = 60000;
    	request.ProtocolVersion = new System.Version("1.1");
    }
    /cethie
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    I'm not sure why this was in the mobile forum other than its a compact framework project. You will probably find more help in the .net forum. I've moved it over here, the mods in this forum should be able to help or point you in the right direction.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I think you need to be setting the ContentLength header (set it data.Length size probably)

      EDIT: Although if it only happens sometimes, check to make sure the url object is always a correct item? You should be able to debug this?

      Comment

      • cethie
        New Member
        • Jan 2010
        • 6

        #4
        Originally posted by Plater
        I think you need to be setting the ContentLength header (set it data.Length size probably)

        EDIT: Although if it only happens sometimes, check to make sure the url object is always a correct item? You should be able to debug this?
        I did try setting contentlength and changing various properties. 2 other things I noticed:

        1) posted data is always empty even though writing to the stream succeeds.
        2) the 'uncatcheable' exception from the inner thread is coming from a certain set_ContentLeng th method within the framework! This is consistent whether or not I set content length in my request object,

        Comment

        • cethie
          New Member
          • Jan 2010
          • 6

          #5
          Hello RedSon,

          Thanks I will follow it up on the other thread.

          /cethie

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Does setting those things in SetRequestPrope rties actually translate back to the original webrequest like that?

            Edit: What URL are you using? I just tested it and it worked fine*


            *fine being that it threw a "server violated protocol" webexception, even though it really didn't and all data transfered correctly.

            Comment

            • cethie
              New Member
              • Jan 2010
              • 6

              #7
              The request object is passed to SetRequestPrope rties by reference. So should be fine. I checked that the properties are getting set.

              I am connecting to http://<my_ip_addres s>/test/test.aspx from my Windows Mobile device emulator. If I visit the same URL manually from IE on the device, it works fine.

              Comment

              Working...