473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 3107
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

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

Similar topics

4
by: Gary Petersen | last post by:
For the benefit of others, I want to show how to do an HTTP POST request using fsockopen(). I banged my head against a wall for two days trying to figure this out. I even went to http://php.net/...
0
by: Spud | last post by:
<?php // pullpage function by Nick bouton http://www.nickbouton.com/. $CustomerID = "IDHERE"; $method = "POST"; $host = "xml.mydata.com"; $usepath = "/xml.asp"; //print all vars in an...
7
by: Dodger | last post by:
Apologies if the is the wrong NG. But I have trapped a GET issued by a webpage on a button press and I would like to issue the GET programatically as-and-when I wish. I wondered what the text...
0
by: Owen | last post by:
Hello everyone, I am using VS.NET 2003(Trandition Chinese) Edition, and httpLook software for checking http requests. I found a problem that the following programs don't really "POST". These...
0
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
5
by: David Lozzi | last post by:
Howdy, I wrote a web service in .Net for my customer. My customer has another vendor who now has to consume it but they are not using Visual Studio. Most of their pages are jsp, and they said...
3
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
1
by: Arfeen | last post by:
Hi All, I need help again ..... I have an asp.net web page which I hit using the "HTTP POST" method. My ASP.NET page is a basic hello world example with the following code: private void...
2
by: =?Utf-8?B?cGhlbmdsYWk=?= | last post by:
I am not sure really how to explain this but here goes. I am creating some new web services and I want to be able to post data like the following: <Data> <some value>1</some value> <more...
6
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.