Results 1 to 3 of 3
  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 online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,399
    Rep Power
    25

    Default

    Is this post the same as this one: POST request using HttpUrlConnection

    Why post it again?

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

    Default

    After clicking the posting button it was taking a lot of time (my connection sucks). So I left the computer and when I came back it was still loading and I refreshed the page. Sorry for that, I tried to remove it myself but I couldn't find that option. If you could remove it for me would be great.

Similar Threads

  1. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 3
    Last Post: 07-06-2011, 04:36 PM
  2. Authenticating for a java POST request
    By stodds in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2010, 12:11 AM
  3. HTTP Post Request from Java
    By Darrarski in forum New To Java
    Replies: 10
    Last Post: 06-05-2010, 05:22 PM
  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
  •