473,418 Members | 2,063 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,418 software developers and data experts.

How to send data from a client to a server via the POST method.

I am trying to send a few NameValuePair pieces of data to a server. I wanted to know how I can turn my current GET request into a POST request so that the data encapsulation is not done so merely in a URL string. Here is what I am using now:
Expand|Select|Wrap|Line Numbers
  1. /**
  2.      * Connect to the supplied URL using GET     
  3. * @param urlString
  4.      * @return true for we receive HTTP_OK from http call 
  5.      */
  6.  
  7.     private boolean connectToURL(Location loc)
  8.     {
  9.  
  10.         double latitude = loc.getLatitude();
  11.         double longitude = loc.getLongitude();
  12.         //double accuracy = loc.getAccuracy();
  13.         //double altitude = loc.getAltitude();
  14.         //double bearing = loc.getBearing();
  15.  
  16.         CoordinateConversion cc = new CoordinateConversion();
  17.         String utmconverted = cc.latLon2UTM(latitude, longitude);
  18.  
  19.        String uArg="key="+URLEncoder.encode(key)+"&tag="+URLEncoder.encode(tag)+"&utm="+URLEncoder.encode(""+utmconverted)/*+"&bearing="+URLEncoder.encode(""+bearing)"&accuracy="+URLEncoder.encode(""+accuracy)+"&alt="+URLEncoder.encode(""+altitude)*/;
  20.  //       String uArg="key="+URLEncoder.encode(key)+"&tag="+URLEncoder.encode(tag)+"&latitude="+URLEncoder.encode(""+latitude)+"&longitude="+URLEncoder.encode(""+longitude);
  21.  
  22.         String uString=url+uArg; //Assumes url ends with "/"
  23.         Log.e("APP", "Output = "+uString);
  24.         boolean res=false;
  25.  
  26.         try
  27.         {        
  28.  
  29.  
  30.  
  31.             URL url=new URL(uString);
  32.  
  33.  
  34.             URLConnection uConnection = url.openConnection();
  35.  
  36.             if (!(uConnection instanceof HttpURLConnection))                     
  37.                 return false;
  38.  
  39.             HttpURLConnection connection = (HttpURLConnection) uConnection;
  40.             connection.setRequestMethod("GET");
  41.             connection.setInstanceFollowRedirects(true);
  42.             connection.setAllowUserInteraction(false);
  43.             connection.connect(); 
  44.  
  45.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) 
  46.             {
  47.                 res=true;
  48.             }    
  49.             connection.disconnect();
  50.         }
  51.         catch (Exception ex)
  52.         {
  53.             res=false; // If disconnect fails
  54.         }
  55.  
  56.         return res;
  57.  
  58.  
  59.  
  60.  
  61.     }
May 24 '10 #1
2 2055
jkmyoung
2,057 Expert 2GB
Can't you simply set:
connection.setRequestMethod("POST");
?

Is there a specific problem you are having?
May 25 '10 #2
@jkmyoung
Will that not just send the URL again though? Thanks for the reply!
May 25 '10 #3

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

Similar topics

2
by: WIWA | last post by:
Hi, I want to make a UDP client server application that is conform to RFC868 (Time protocol). Both the UDP client and UDP server are in a test phase. The question is: when I add ...
15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
2
by: sangeetha | last post by:
Hi, How to send a request using post method from browser to web server using javascript? Sangeetha
4
by: milkyway | last post by:
Hello everyone, I have a few values and variables that I want to post to a server (without using a SUBMIT button). Is there a way to post data from within javascript - do sockets or connections...
1
by: Vishal | last post by:
Hello, I need to send some data via the post method from my web aspx page to an asp page. Does anybody know how I can do that? Please advise me. Thanks
5
by: Vishal | last post by:
Hello, I already asked this question in the ASP.NET forums, but no help came. So I am hoping that somebody can help me out. This is really very URGENT me. For my e-commerce application, I...
2
ronverdonk
by: ronverdonk | last post by:
I hope someone can show me the right direction for solving my problem. I have program where a user can enter text in a <textarea>. I want to give that user the possibility to have a pre-look at...
4
by: Velhari | last post by:
Hi all, We all know in GET method upto 2048 characters can be appended into URL. In the Same way what is the limit of sending data to server by using POST Method. Why asking this question...
0
by: Rob Williscroft | last post by:
Lorenzo Stella wrote in news:7956f925-1037-49ea-a360-b58d627ffb20 @z24g2000prf.googlegroups.com in comp.lang.python: Curl maybe, http://pycurl.sourceforge.net/ Rob. --...
5
by: NitinSawant | last post by:
Hello, I'm beginner to php (actually i'm java/jsp developer), What i'm trying to do is Accept parameters from the HTML file and write them to a newly created file using php, I wrote...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.