8 Replies - 1956 Views - Last Post: 14 January 2011 - 04:26 PM Rate Topic: -----

#1 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

HttpURLConnection posting

Posted 13 January 2011 - 10:17 PM

How would I do exactly what this does (in html) in java with the HttpURLConnection:

<form id="login" action="https://www.deviantart.com/users/login" method="post"> 
                  <input type="hidden" name="ref" value="https://www.deviantart.com/users/loggedin" /> 
                  
         <table align="center" cellpadding="10"> 
         <tr> 
            <td style="text-align:right"> 
                 <label> 
                     <strong>Deviant Name</strong> 
                     <input class="text" type="text" name="username" autofocus="autofocus"/> 
                 </label> 
            </td> 
         </tr> 
         <tr> 
            <td style="text-align:right"> 
                 <label> 
                     <strong>Password</strong> 
                     <input class="text" type="password" name="password"/> 
                 </label> 
            </td> 
         </tr> 
         <tr> 
            <td align="center"><input class="button" type="submit" value="Log In"/></td> 
         </tr> 
         <tr> 
            <td> 
                 <label for="reusetoken"><input type="checkbox" class="checkbox" id="reusetoken" name="reusetoken" value="1" checked="checked" style="vertical-align: middle"/> Don't log me out of other browsers</label> 
            </td> 
         </tr> 
         </table> 
         </form> 
           </div> 


I am new to using httpurlconnection, but this is what I currently have and it doesn't work. (It's supposed to redirect to either a correct login page or an incorrect one.)

HttpURLConnection conn = (HttpURLConnection) new URL("https://www.deviantart.com/users/login").openConnection();
			HttpURLConnection.setFollowRedirects(true);
			String agent = "Mozilla/4.0";
			String rawData = "ref=https://www.deviantart.com/users/loggedin&username=" + username + "&password=" + password + "&reusetoken=1";
			String type = "application/x-www-form-urlencoded";
			String encodedData = URLEncoder.encode(rawData);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty( "User-Agent", agent );
			conn.setRequestProperty( "Content-Type", type );
			conn.setRequestProperty( "Content-Length", 
							String.valueOf(encodedData.length()) );
			OutputStream os = conn.getOutputStream();
			os.write( encodedData.getBytes() );
			os.flush();
			int rc = conn.getResponseCode();
			if (rc == HttpURLConnection.HTTP_OK) {
				System.out.println(conn.getURL().toExternalForm());
			}


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: HttpURLConnection posting

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: HttpURLConnection posting

Posted 13 January 2011 - 10:23 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: HttpURLConnection posting

Posted 13 January 2011 - 10:41 PM

Moved to J2EE forum
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: HttpURLConnection posting

Posted 13 January 2011 - 10:44 PM

Actually, more of a java.net question than Java EE.

Moved back to Java. :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: HttpURLConnection posting

Posted 13 January 2011 - 10:48 PM

View Postmacosxnerd101, on 13 January 2011 - 11:44 PM, said:

Actually, more of a java.net question than Java EE.

Moved back to Java. :)

If you say/think so :)
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: HttpURLConnection posting

Posted 13 January 2011 - 10:50 PM

View Postpbl, on 14 January 2011 - 12:48 AM, said:

If you say/think so :)


Yah, actually, I moved it out of Java Programmers originally. I just didn't leave a not or anything :P Probably should now.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_manbearpig101*


Reputation:

Re: HttpURLConnection posting

Posted 14 January 2011 - 01:24 PM

Well my specific problem is that it isn't redirecting (to an error or success page), so I suspect I did everything wrong.
Was This Post Helpful? 0

#8 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 12-October 10

Re: HttpURLConnection posting

Posted 14 January 2011 - 02:30 PM

You must call the connect method in HttpURLRequest to actually establish the connection, just flushing won't do the trick because you're actually flushing to a unwritten buffer. You can always connect to a HTTP port with URLRequest, which does not require an established connection and just streams data, but has its risks. Although this works for many servers, some just don't accept that. Then you should call disconnect when no more data are being exchanged.
Also, by encoding after the parameters are concatenate, you are encoding also the '&' (if it is UTF-8).
Was This Post Helpful? 0
  • +
  • -

#9 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: HttpURLConnection posting

Posted 14 January 2011 - 04:26 PM

Okay edit for a third time, I got it all working, thank you everyone!

Just in case anyone wants to know, .getHeaderField("Location") gets the redirection location. :)

This post has been edited by Manbearpig101: 14 January 2011 - 04:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1