403 forbidden response to login
Is the problem that google takes steps to prevent logging in as done
below? If so, which https URL would be better to test this class?
Aside from some deprecated code, what's wrong with the below example,
from <http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-
program.htm>? Is there any particular reason as to why this results in
"forbidden"?
thufir@arrakis:~$
thufir@arrakis:~$ java -jar NetBeansProjects/rest/dist/rest.jar
Resp Code:403
Resp Message:Forbidden
Exception in thread "main" java.io.IOException: Server returned HTTP
response code 403 for URL https://www.google.com/accounts/ClientLogin
at gnu.java.net.protocol.http.HTTPURLConnection.getInputStream
(libgcj.so.90)
at rest.Main.login2(Main.java:55)
at rest.Main.main(Main.java:29)
thufir@arrakis:~$
thufir@arrakis:~$ cat NetBeansProjects/rest/src/rest/Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rest;
//imports omitted
/**
*
* @author thufir
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws MalformedURLException,
IOException {
login2(new URL("https://www.google.com/accounts/ClientLogin"),
"hawat.thufir@gmail.com", "password");
}
private static void login2(URL url, String user, String password)
throws MalformedURLException, IOException {
HttpsURLConnection connection = (HttpsURLConnection)
url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-
form-urlencoded");
connection.setRequestProperty("User-Agent", "Mozilla/4.0
(compatible; MSIE 5.0; Windows 98; DigExt)");
HttpsURLConnection.setFollowRedirects(true);
DataOutputStream output = new DataOutputStream
(connection.getOutputStream());
@SuppressWarnings("deprecation")
String query = "UserID=" + URLEncoder.encode
("hawat.thufir@gmail.com");
query += "&";
query += "password=" + URLEncoder.encode("password");
connection.setRequestProperty("Content-length", String.valueOf
(query.length()));
System.out.println("Resp Code:" + connection.getResponseCode());
System.out.println("Resp Message:" + connection.getResponseMessage
());
// get ready to read the response from the cgi script
DataInputStream input = new DataInputStream
(connection.getInputStream());
// read in each character until end-of-stream is detected
for (int c = input.read(); c != -1; c = input.read()) {
out.print((char) c);
}
input.close();
}
private static void login(URL url, String user, String password)
throws MalformedURLException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader
(url.openStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
in.close();
out.println(sb);
}
}
thufir@arrakis:~$
However, when I run it from within Netbeans the result is slightly
different:
run:
Exception in thread "main" java.lang.IllegalStateException: Already
connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty
(HttpURLConnection.java:2177)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty
(HttpsURLConnectionImpl.java:296)
at rest.Main.login2(Main.java:49)
at rest.Main.main(Main.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
thanks,
Thufir