Results 1 to 4 of 4
  1. #1
    turk is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default POST request using HttpUrlConnection

    Hello everyone,

    I had been trying to send POST requests over the network. Unfortunately I wasn't very successful. I'm working on a Java Applet to post on a given forum from my application, without having to open the browser

    The request was sent properly and the server returns me 200 code and the HTML of the website but it doesn't "post" anything.
    More exactly it returns the "posting" page and not the code from the "posted" page.

    Thank you for your time.

    Here is my code:

    Java Code:
    String agent = "Mozilla/4.0";
    		String rawData = "name=joe&subject=guessme&text=hum";
    		String type = "application/x-www-form-urlencoded";
    		URL url = new URL("http://www.mywebsite.com/posting.php");
    		HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    		
    		String encodedData = URLEncoder.encode(rawData, "UTF-8"); // user-supplied
    
    		try {
    			urlConnection.setDoOutput(true);
    			urlConnection.setUseCaches(false);
    			urlConnection.setDoInput(true);
    			urlConnection.setAllowUserInteraction(false);
    			urlConnection.setRequestMethod("POST");
    			urlConnection.setRequestProperty( "User-Agent", agent );
    			urlConnection.setRequestProperty( "Content-Type", type );
    			urlConnection.setRequestProperty( "Content-Length", Integer.toString(encodedData.length()) );
    
    			OutputStream os = urlConnection.getOutputStream();
    			os.write(encodedData.getBytes());
    	    
    	    		int rc = urlConnection.getResponseCode();
    	    		System.out.println(rc); // Always 200
    	    		
    	    		BufferedReader in = new BufferedReader(
    	    				   new InputStreamReader(urlConnection.getInputStream()));
    	    				String line = null;
    	    				while ((line = in.readLine()) != null)
    	    				{
    	    				   System.out.println(line);
    	    				}
    		}
    		catch( IOException e ){
    			// handle the error here
    		}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Perhaps the server can detect your request is not coming from a browser and does not want to allow machine generated POSTs.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Try calling flush() on your output stream, and a suggestion: don't leave exception handling catch statements empty

  4. #4
    turk is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you for your help and your quick reply.

    I added the flush statement and I'm still getting the same response.

    I think Norm is right and the server has some kind of security system.

    Now I can think of two different scenarios:
    1.- The server side application may be setting a cookie or a session on the browsers to prevent machine generated posts (no logging in is required for posting, though).
    2.- Something in the POST parameters that I seted is wrong.

    Can anyone think on any other security mechanism?
    I will get back to you if I can figure it out.

    Regarding the exception handling, I wrote this code as a first approach, just to see if the server was responding properly.

Similar Threads

  1. Authenticating for a java POST request
    By stodds in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2010, 12:11 AM
  2. HTTP Post Request from Java
    By Darrarski in forum New To Java
    Replies: 10
    Last Post: 06-05-2010, 05:22 PM
  3. Invocation of Restful services thru POST request
    By userj2ee in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2010, 11:40 AM
  4. Replies: 4
    Last Post: 10-15-2008, 01:59 PM
  5. First post as per request
    By happyknappy in forum Introductions
    Replies: 3
    Last Post: 07-30-2008, 01:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •