Guarda ti allego il codice perchè ormai sono disperato ahaha :
vado a creare l'oggetto e invocare il metodo Connetti() attraverso il clicklistener ma da disperati errori @.@ la cosa buffa è che questa classe su netbeans funziona tranquillamente , ho preso questa classe da un sito e l'ho adattata alle mie esigenze il problema è che su android come ripeto da una pentola di errori boh
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Connessione{
private List<String> cookies;
private HttpURLConnection conn;
URL obj;
String url;
String gmail;
Connessione http;
private final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";
public Connessione() {
url = "
http://it.ogame.gameforge.com/main/logi";
gmail = "
http://s130-it.ogame.gameforge.com";
try {
obj = new URL(url);
} catch (MalformedURLException ex) {
Logger.getLogger(Connessione.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void Connettiti() throws Exception {
http = new Connessione();
// make sure cookies is turn on
CookieHandler.setDefault(new CookieManager());
// 1. Send a "GET" request, so that you can extract the form's data.
String page = http.GetPageContent(url);
String postParams = http.getFormParams(page, "user", "pass");
postParams = "kid=&" + postParams;
// 2. Construct above post's content and then send a POST request for
// authentication
http.sendPost(url, postParams);
// 3. success then go to gmail.
System.out.println(http.conn.getURL());
}
private void sendPost(String url, String postParams) throws Exception {
conn = (HttpURLConnection) obj.openConnection();
// Acts like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "it.ogame.gameforge.com");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3");
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(72));
conn.setDoOutput(true);
conn.setDoInput(true);
// Send post request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
BufferedReader in
= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// System.out.println(response.toString());
}
private String GetPageContent(String url) throws Exception {
URL obj = new URL(url);
conn = (HttpURLConnection) obj.openConnection();
// default is GET
conn.setRequestMethod("GET");
conn.setUseCaches(true);
// act like a browser
conn.setRequestProperty("(Request-Line)", "GET /game/reg/login2.php?data=S%2F7PZw5a2GTJtRLyojv2TSo33ci3nX0I0ZYPOraxLTI0URcXAdDgUSBI2BESamECUsOW2E0tLY0YOlyRXPeJxWTH60Hs8KSdZldnc9gxhObAVeeJtGBLjRP3dN9CNwMpy2xV0D3rQUnUm7KLITYjeywg6jBlZcOGcFK1mYrtPok%3D HTTP/1.1");
conn.setRequestProperty("Host", "s130-it.ogame.gameforge.com");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Referer", "
http://it.ogame.gameforge.com");
if (cookies != null) {
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
}
System.out.println("\nSending 'GET' request to URL : " + url);
BufferedReader in
= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Get the response cookies
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
}
public String getFormParams(String html, String username, String password)
throws UnsupportedEncodingException {
System.out.println("Extracting form's data...");
Document doc = Jsoup.parse(html);
// Google form id
Element l_server = doc.getElementById("serverLogin");
Element l_user = doc.getElementById("usernameLogin");
Element l_password = doc.getElementById("passwordLogin");
Elements inputElements = new Elements();
inputElements.add(l_server);
inputElements.add(l_user);
inputElements.add(l_password);
List<String> paramList = new ArrayList<String>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("uni")) {
value = "s130-it.ogame.gameforge.com";
}
if (key.equals("login")) {
value = username;
} else if (key.equals("pass")) {
value = password;
}
System.out.println(key + " " + value);
paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
}
// build parameters list
StringBuilder result = new StringBuilder();
for (String param : paramList) {
if (result.length() == 0) {
result.append(param);
} else {
result.append("&" + param);
}
}
return result.toString();
}
public List<String> getCookies() {
return cookies;
}
public void setCookies(List<String> cookies) {
this.cookies = cookies;
}
}