File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HttpURLConnection POST problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpURLConnection POST problem" Watch "HttpURLConnection POST problem" New topic

HttpURLConnection POST problem

Matthias Edrich

Joined: Apr 16, 2004
Posts: 3
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.
(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, 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 = ""
*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

Joined: Apr 16, 2004
Posts: 3
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){
I agree. Here's the link:
subject: HttpURLConnection POST problem