HttpClient登陆网站参考_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > HttpClient登陆网站参考

HttpClient登陆网站参考

 2010/12/25 0:04:42  a38876399  http://a38876399.javaeye.com  我要评论(0)
  • 摘要:staticfinalStringLOGON_SITE="xxx.com";staticfinalintLOGON_PORT=80;HttpClientclient=newHttpClient();client.getHostConfiguration().setHost(LOGON_SITE,LOGON_PORT);PostMethodpost=newPostMethod("/terminal/system/login.action?loginName=XXX&
  • 标签:client 网站

?

static final String LOGON_SITE = "xxx.com";

 static final int LOGON_PORT = 80;

 

HttpClient client = new HttpClient();
  client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);

  PostMethod post = new PostMethod("/terminal/system/login.action?loginName=XXX&password=XXX&oem=terminal");

  // 设置登陆需要的几个参数
  // NameValuePair name = new NameValuePair("loginName","XXX");
  // NameValuePair pass = new NameValuePair("password","XXX");
  // NameValuePair em = new NameValuePair("oem","terminal");
  // post.setRequestBody(new NameValuePair[]{name,pass,oem});

  client.executeMethod(post);

  System.out.println(post.getStatusLine().toString());

  post.releaseConnection();

  // 检查是否重定向

  int statuscode = post.getStatusCode();
  
  if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||

     (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

   // 读取新的URL地址

   Header header = post.getResponseHeader("location");

   if (header != null) {

    String newuri = header.getValue();

    if ((newuri == null) || (newuri.equals("")))

     newuri = "/";

    GetMethod redirect = new GetMethod(newuri);

    client.executeMethod(redirect);

    System.out.println("Redirect:" + redirect.getStatusLine().toString());

    redirect.releaseConnection();

   } else
    System.out.println("Invalid redirect");
  }
文章出处:http://xieruilin.javaeye.com/blog/746306
发表评论
用户名: 匿名