Results 1 to 3 of 3
- 07-06-2011, 04:08 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
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 }
- 07-06-2011, 04:25 PM #2
Is this post the same as this one: POST request using HttpUrlConnection
Why post it again?
- 07-06-2011, 04:29 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
POST request using HttpUrlConnection
By turk in forum NetworkingReplies: 3Last Post: 07-06-2011, 04:36 PM -
Authenticating for a java POST request
By stodds in forum Advanced JavaReplies: 1Last Post: 12-08-2010, 12:11 AM -
HTTP Post Request from Java
By Darrarski in forum New To JavaReplies: 10Last Post: 06-05-2010, 05:22 PM -
how to POST data to server using HttpURLConnection ??
By dhana in forum NetworkingReplies: 4Last Post: 10-15-2008, 01:59 PM -
First post as per request
By happyknappy in forum IntroductionsReplies: 3Last Post: 07-30-2008, 01:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks