• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

HttpURLConnection POST problem

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi y'all,
I am having trouble posting data to a URL.
There's a webpage form out there on the net, which allows me to enter
data and enter it into a database. The for uses the "POST" method to submit the data to its cgi.
Well, now I want to create a servlet, which will post that data directly to the above mentioned cgi.
Here is what I have found out:
1. when I enter the direct url into my browser, the data is inserted successfully. I.e. http://sunnyday.com/binny.cgi?note=blablabla+blablabla
(This is a GET, right).
2. I don't want to use GET, because I found that this limits the amount of data I can "send" to the script. The text I need to send is about 6000 characters long.
3. To be able to post the data / or use the direct URL, I need to log in. I.e. the browser pops up a small authentication window.
4. I have code, which I think covers the authentication, but, as it turns out somehow doesn't submit my data correctly. I have attached my code below. It seems that the code knows where to go... i.e. Goes to the CGI. But is also seems that the CGI can't see the variables I am Posting to it, because, it acts as if I hadn't given it anything. (When I type in http://sunnyday.com/binny.cgi, it says that it can't do anything without parameters. WIth my code, I get the same answer back).
Here's my question: What am I doing incorrectly? How can I POST data to this CGI? How can I give it this large amount of data?

Hi Marilyn, in case you see this :-) Hope you're doing well.
*I provide the following variables:
*username = myuser
*password = mypassword\
*spWebURLString = "http://www.sunnyday.com/cgi-bin/web.cgi"
*spWebParameters = "note=blablabla+blablal+dsf&case=546345&id=4545&docid=7fg&save=Save";
Authenticator.setDefault(new SimpleAuthenticator(username, password));
HttpURLConnection uc = (HttpURLConnection)(new URL(spWebURLString)).openConnection();
// *** POST-method ***
OutputStream os = uc.getOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(os);
System.err.println(uc.getResponseCode() + " and " + uc.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line;
while ((line = in.readLine()) != null) System.out.println(line);
Matthias Edrich
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the problem with the help of others. Here is the tip I got:
/** post data to an url that requires authorization and get a response as well
@param urlstr the complete url, including http://
@param username the username
@param password the password
@param parameters the parameters in unencoded-url format (param1=value¶m2=value etc.)
@return the response generated by the url from the post
public static String postDataAuthorize(String urlstr, String parameters, String username, String password)
throws IOException
URL url = new URL(urlstr);
HttpURLConnection hpcon = null;

String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
hpcon = (HttpURLConnection) url.openConnection();
hpcon.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
hpcon.setRequestProperty ("Authorization", "Basic " + encoding);
//hpcon.setRequestProperty("User-Agent", "MLI/Java development environment");
hpcon.setRequestProperty("Content-Language", "en-US");
hpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hpcon.setUseCaches (false);
DataOutputStream printout = new DataOutputStream (hpcon.getOutputStream ());
printout.writeBytes (parameters);
printout.flush ();
printout.close ();

// getting the response is required to force the request, otherwise it might not even be sent at all
BufferedReader in = new BufferedReader(new InputStreamReader(hpcon.getInputStream()));
String input;
StringBuffer response = new StringBuffer(256);

while((input = in.readLine()) != null) {
response.append(input + "\r");
return response.toString();

} catch(Exception e){
throw new IOException(e.getMessage());
} finally {
if(hpcon != null){
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic