登录尝试使用HTTPS的网页失败

时间:2013-12-28 13:54:09

标签: java android https html-parsing

我正在尝试开发一个解析某个强制https的网页的Android应用程序。直到现在,我没有困难使用jsoup解析数据。但是,尽管付出了很多努力,我还是无法使用我的应用程序登录网页。我的工作主要基于我在网上找到的这个教程:How to automate login a website – Java example

我开始使用firebug和firefox检查工具来分析登录机制。我找到了登录所需的cookie和帖子参数。虽然我相信我可以完全模仿登录行为,但我总是得到“内部服务器错误500”。我开始认为可能存在某种保护机制。因为当我尝试使用fiddler和firefox检查工具模仿登录时,我得到了相同的结果。

我发布了我从fiddler获得的HTTPS / POST方法的原始数据。谁能告诉我可能会缺少什么?

POST https://somewebsite.com/login HTTP/1.1
Host: somewebsite.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: https://somewebsite.com/login?returnurl=%2f
Cookie: ASP.NET_SessionId=o04pvruobqbjhdm5tmyfh1ye; __RequestVerificationToken=iuPsOmyPfl9qJnGKf4NZpnb2Ps0CvZZkQP6NDkTbI9ZQKLVkahAE3qpa-m_Xz68lXJ_GS-JWAbjWx-Y6iwbFusU2k5AKJbNMDJyyxV6SpPc1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 216

__RequestVerificationToken=xmbUJeF3slMbYYEJWBl4bLDDjjB2USgbiFg3Oy2j9VUX7rJgFrRmEMyBs5XhkMPH9G3l_j7bkrIBEtScAu-bJaTRq2aHwoow0TNOfsEHxbY1&ReturnUrl=%2F&UserName=username&Password=password&RememberMe=false

这就是我获取cookies的方式:

static private List<String> cookies;

static public 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");

    int responseCode = conn.getResponseCode();

    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
    cookies = conn.getHeaderFields().get("Set-Cookie");

    return response.toString();
}

这是我收到cookie后发布请求的方式:

static private int 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", "somewebsite.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", "tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3");
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    conn.setRequestProperty("Referer", "https://somewebsite.com/login?returnurl=%2f");

    for (String cookie : cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";")[0]);
    }

    conn.setRequestProperty("Connection", "keep-alive");


    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    String contentLength = Integer.toString(postParams.length());
    conn.setRequestProperty("Content-Length", contentLength);

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

    try {
    BufferedReader in =
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return responseCode;
}

0 个答案:

没有答案