通过java

时间:2016-08-22 19:01:49

标签: java android post login httpurlconnection

我尝试编写一个Android应用程序登录页面(goofbid.com)并从中获取一些信息。

我尝试了各种代码,但没有任何效果。其中一个最有希望的代码来自mkyong.com。 我编辑了这段代码,这是我的最后一个结果(包括一些无用的东西):

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
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 LoginGoofbid {

private List<String> cookies;
private HttpsURLConnection conn;

private final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception {

String url = "https://www.goofbid.com/auth";
String sniper = "https://www.goofbid.com/account/sniper";

LoginGoofbid http = new LoginGoofbid();

// 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);


// 2. Construct above post's content and then send a POST request for
// authentication
http.sendPost(url, postParams);

// 3. success then go to sniper.
String result = http.GetPageContent(sniper);
System.out.println(result);
}

private String GetPageContent(String url) throws Exception {

URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();

// default is GET
conn.setRequestMethod("GET");

conn.setUseCaches(false);

// act like a browser
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", "en-US,en;q=0.5");
if (cookies != null) {
    for (String cookie : this.cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
    }
}
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
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();

// Get the response cookies
setCookies(conn.getHeaderFields().get("Set-Cookie"));

return response.toString();

}

private void sendPost(String url, String postParams) throws Exception {

URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();

// Acts like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "www.goofbid.com:443");
       conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Encoding","gzip, deflate, br");
conn.setRequestProperty("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
for (String cookie : this.cookies) {
    conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
conn.setRequestProperty("Origin", "http://www.goofbid.com");
conn.setRequestProperty("Referer", "http://www.goofbid.com/");
conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");


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();

}



public String getFormParams(String html)
    throws UnsupportedEncodingException {

System.out.println("Extracting form's data...");


List<String> paramList = new ArrayList<String>();

    paramList.add("referer" + "=" + URLEncoder.encode("?", "UTF-8"));
    paramList.add("username" + "=" + URLEncoder.encode("MYUSERNAME", "UTF-8"));
    paramList.add("password" + "=" + URLEncoder.encode("MYPASSWORD", "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;
}

}

它不起作用,只是将我重定向到登录页面。

我试图用wireshark来嗅探我的通信,但不知怎的,我无法从中获取正确的信息所以我尝试使用chrome的Live Http Headers,但这只显示了浏览器交互而不是eclipse / java交互。但至少我可以获得“帖子”的(错误?)标题信息。

也许你有一些提示或教程。或者可以修复此代码...

0 个答案:

没有答案