469,282 Members | 2,119 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

HTTP POST Vs GET

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

Expand|Select|Wrap|Line Numbers
  1. /*
  2.     Environment
  3.     -------------
  4.     * NET CF 2.0
  5.     * WM 5.0(USA Mobile Pocket PC Emulator)
  6.     * Windows XP Professional SP2
  7.     * VS 2008
  8. */
  9.  
  10. /*
  11.     The exception
  12.     ------------------
  13.    at System.Net.HttpWebRequest.set_ContentLength(Int64 value)
  14.    at System.Net.HttpWebRequest.BufferConnectStream.WritingSucceeds()
  15.    at System.Net.HttpWriteStream.doClose()
  16.    at System.Net.HttpWriteStream.Finalize()
  17. */
  18.  
  19. public static string DoPost(string url)
  20. {
  21.     // initialize from variables
  22.     string responseString = string.Empty;
  23.     ASCIIEncoding encoding = new ASCIIEncoding();
  24.     HttpWebResponse response;
  25.     byte[] data = encoding.GetBytes("dummy");
  26.     StreamReader reader;
  27.     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  28.  
  29.     //do the processing
  30.     SetRequestProperties(request, "POST"); // SETTING METHOD TO POST HERE
  31.     request.GetRequestStream().Write(data, 0, data.Length);
  32.     request.GetRequestStream().Close();
  33.     response = (HttpWebResponse)request.GetResponse();
  34.     reader = new StreamReader(response.GetResponseStream());
  35.     responseString = reader.ReadToEnd();
  36.  
  37.     //clean up
  38.     response.Close();
  39.     response.GetResponseStream().Close();
  40.     response.GetResponseStream().Dispose();
  41.     reader.Close();
  42.     reader.Dispose();
  43.     reader = null;
  44.     response = null;
  45.     request = null;
  46.     encoding = null;
  47.  
  48.     //return
  49.     MessageBox.Show("POST SUCCESS");
  50.     return responseString;
  51.  
  52. }    
  53.  
  54. public static string DoGet(string url)
  55. {
  56.     // initialize from variables
  57.     string responseString = string.Empty;
  58.     ASCIIEncoding encoding = new ASCIIEncoding();
  59.     HttpWebResponse response;
  60.     byte[] data = encoding.GetBytes("dummy");
  61.     StreamReader reader;
  62.     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  63.  
  64.     //do the processing
  65.     SetRequestProperties(request, "GET");  // SETTING METHOD TO GET HERE
  66.     response = (HttpWebResponse)request.GetResponse();
  67.     reader = new StreamReader(response.GetResponseStream());
  68.     responseString = reader.ReadToEnd();
  69.  
  70.     //clean up
  71.     response.Close();
  72.     response.GetResponseStream().Close();
  73.     response.GetResponseStream().Dispose();
  74.     reader.Close();
  75.     reader.Dispose();
  76.     reader = null;
  77.     response = null;
  78.     request = null;
  79.     encoding = null;
  80.  
  81.     //return
  82.     MessageBox.Show("GET SUCCESS"); 
  83.     return responseString;
  84.  
  85. }
  86.  
  87. private static void SetRequestProperties(HttpWebRequest request, string s)
  88. {
  89.     request.Method = s;
  90.     request.AllowWriteStreamBuffering = true;
  91.     request.KeepAlive = false;
  92.     request.ContentType = "application/x-www-form-urlencoded";
  93.     request.SendChunked = false;
  94.     request.Credentials = CredentialCache.DefaultCredentials;
  95.     request.UserAgent = "my mobile user agent";
  96.     request.Timeout = 60000;
  97.     request.ProtocolVersion = new System.Version("1.1");
  98. }
  99.  
/cethie
Jan 4 '10 #1
6 2833
RedSon
5,000 Expert 4TB
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.
Jan 4 '10 #2
Plater
7,872 Expert 4TB
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?
Jan 4 '10 #3
cethie
6
@Plater
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_ContentLength method within the framework! This is consistent whether or not I set content length in my request object,
Jan 5 '10 #4
cethie
6
Hello RedSon,

Thanks I will follow it up on the other thread.

/cethie
Jan 5 '10 #5
Plater
7,872 Expert 4TB
Does setting those things in SetRequestProperties 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.
Jan 5 '10 #6
cethie
6
The request object is passed to SetRequestProperties by reference. So should be fine. I checked that the properties are getting set.

I am connecting to http://<my_ip_address>/test/test.aspx from my Windows Mobile device emulator. If I visit the same URL manually from IE on the device, it works fine.
Jan 5 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Gary Petersen | last post: by
7 posts views Thread by Dodger | last post: by
reply views Thread by WIWA | last post: by
5 posts views Thread by David Lozzi | last post: by
3 posts views Thread by JansenH | last post: by
1 post views Thread by Arfeen | last post: by
2 posts views Thread by =?Utf-8?B?cGhlbmdsYWk=?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.