我得到了相同的响应html,我曾经在httpclient中发布参数

时间:2014-06-04 03:42:21

标签: java html httpclient

当我尝试将用户名和密码参数发布到登录页面时,我得到相同的响应HTML。我已按照此处提供的教程进行操作。

http://www.mkyong.com/java/apache-httpclient-examples/

我能够成功地为gmail页面执行此操作,但我无法为IBM Partner World Page执行此操作。这是我用于页面登录的java程序

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.CookieHandler;
    import java.net.CookieManager;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.message.BasicNameValuePair;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

    public class tt {

      private String cookies;
      private HttpClient client = HttpClientBuilder.create().build();
      private final String USER_AGENT = "Mozilla/5.0";

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

    String url = "https://www-304.ibm.com/usrsrvc/account/userservices/jsp/login.jsp?persistPage=true&page=/partnerworld/wps/servlet/mem/ContentHandler/partnerworld-public%3Flnk%3Dleft-nav&PD-REFERER=https://www-304.ibm.com/partnerworld/wps/servlet/ContentHandler/partnerworld-public&error=";
    String gmail = "https://www.ibm.com/partnerworld/page/X082763O18037G72";

    // make sure cookies is turn on
    CookieHandler.setDefault(new CookieManager());

    tt http = new tt();

    String page = http.GetPageContent(url);

    List<NameValuePair> postParams = 
               http.getFormParams(page, "username","password");

    http.sendPost(url, postParams);

    String result = http.GetPageContent(gmail);
    System.out.println(result);

    System.out.println("Done");
      }

     private void sendPost(String url, List<NameValuePair> postParams) 
        throws Exception {

    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("Host", "www-304.ibm.com:443");
    post.setHeader("User-Agent", USER_AGENT);
    post.setHeader("Accept", 
             "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    post.setHeader("Accept-Language", "en-US,en;q=0.5");
    post.setHeader("Cookie", getCookies());
    post.setHeader("Accept-Encoding" ,"gzip,deflate,sdch");
    //post.setHeader("Connection", "keep-alive");
    post.setHeader("Referer", "https://www-304.ibm.com/partnerworld/wps/servlet/ContentHandler/pw_com_jnw_index");
    //post.setHeader("Content-Type", "application/x-www-form-urlencoded");

    post.setEntity(new UrlEncodedFormEntity(postParams));

    HttpResponse response = client.execute(post);

    int responseCode = response.getStatusLine().getStatusCode();

    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + postParams);
    System.out.println("Response Code : " + responseCode);

    BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

     System.out.println(result.toString());

  }

  private String GetPageContent(String url) throws Exception {

    HttpGet request = new HttpGet(url);

    request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36");
    request.setHeader("Accept",
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.setHeader("Accept-Language", "en-US,en;q=0.5");

    HttpResponse response = client.execute(request);
    int responseCode = response.getStatusLine().getStatusCode();

    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    // set cookies
    setCookies(response.getFirstHeader("Set-Cookie") == null ? "" : 
                     response.getFirstHeader("Set-Cookie").toString());

    return result.toString();

  }

  public List<NameValuePair> 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 loginform = doc.getElementById("login");
    Elements inputElements = loginform.getElementsByTag("input");

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

    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("username"))
            value = username;
        else if (key.equals("password"))
            value = password;

System.out.println(username+password);
        paramList.add(new BasicNameValuePair(key, value));

    }

    return paramList;
  }

  public String getCookies() {
    return cookies;
  }

  public void setCookies(String cookies) {
    this.cookies = cookies;
  }

}

我得到与用于发布参数的响应相同的HTML。请帮我解决一下这个。 我想知道如何登录这个网站并维持我的会话。

1 个答案:

答案 0 :(得分:1)

我明白了。我使用了Chrome浏览器的开发人员工具,并检查了浏览器试图通过GET和POST请求获取的不同网址。

转到开发工具&gt;网络&gt;并尝试正常访问网页&gt;您将看到所有信息,甚至是从网站返回的cookie和内容。

表单因您想要模仿的页面而异。您应该检查要模仿的登录页面的源(以及其中的不同表单)。使用正确发送用户名和密码的表格。

此外,当您使用开发工具监视网页请求时。检查所有实体。我解决了我的问题  1.检查密码的编码形式。 UrlEncodedFormEntity与我的浏览器使用的不同。

  return "login-form-type=pwd&username="+username+"&password="+password;
 
2。确切的表单url实际上负责发送参数。还有登录参数发送后的下一页。

String url = "https://www-304.ibm.com/pkmslogin.form";
String gmail = "https://www.ibm.com/partnerworld/page/X082763O18037G72";

另外,请确保存储Cookie并将其发回以备下次请求。因为这是你维持会话的方式。