Java中访问页面的HttpClient类

详见:

http://msnvip.iteye.com/blog/261159

 

公司使用单点登陆的时候, 需要在程序中提交用户名,密码到SSO服务器,并且返回ticket的数据信息。来实现登陆。

并且返回给IM系统。

其实这个和ASP里面的抓取数据的代码也比较像。

JAVA中,使用 HttpClient 类来实现这个功能。

首先要导入几个包:

commons-logging-1.1.1-bin

commons-codec-1.3.jar

commons-httpclient-3.1

 

可以在附件下载工程。

 

在程序中设置好地址, 端口,就可以访问返回的数据了 。

 

比较简便的代码:

 

         String associationListUrl=BackendConstants.CLUB_CREATE_URL;

         //associationListUrl = http://club.woyo.com/api/club-creat.action
         HttpClient client = new HttpClient();
         PostMethod method = new PostMethod(associationListUrl);


         method.addParameter("blockId",33005+"");       //频道子板块ID
         method.addParameter("categoryId",14+"");       //社团通用分类ID


         try {
            client.executeMethod(method);
         } catch (HttpException e) {
            throw new Exception("创建商会失败!连接失败: " + e.getMessage());
         } catch (IOException e) {
            throw new Exception("创建商会失败!连接失败: " + e.getMessage());
         }
         
         String returnJson = "";
         try {
            returnJson = method.getResponseBodyAsString();;
            System.out.println("------------------getCreateClub--------------------returnStr--" + returnJson);
            String strJson = returnJson.replace("?(", "").replace(")", "").replace(";", "");
            System.out.println("--------------------------------------strJson--" + strJson);

} catch (IOException e) {
            throw new Exception("创建商会失败!" + e.getMessage());
         }finally{
            //使用完成后要释放链接
            method.releaseConnection();
         }

 

 

 

 

 

 

 

/*

 * Created on 2003-12-7 by Liudong

 */

package http.demo;

 

import java.io.IOException;

 

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;

/**

 * 提交参数演示

 * 该程序连接到一个用于查询手机号码所属地的页面

 * 以便查询号码段1330227所在的省份以及城市

 * @author Liudong

 */

public class SimpleHttpClient {

 

    public static void main(String[] args) throws IOException

    {

        HttpClient client = new HttpClient();

        client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");

 

        HttpMethod method = getPostMethod();//使用POST方式提交数据

        client.executeMethod(method);

       //打印服务器返回的状态

        System.out.println(method.getStatusLine());

        //打印结果页面

        String response =

           new String(method.getResponseBodyAsString().getBytes("8859_1"));

       //打印返回的信息

        System.out.println(response);

        method.releaseConnection();

    }

    /**

     * 使用GET方式提交数据

     * @return

     */

    private static HttpMethod getGetMethod(){

        return new GetMethod("/simcard.php?simcard=1330227");

    }

    /**

     * 使用POST方式提交数据

     * @return

     */

    private static HttpMethod getPostMethod(){

        PostMethod post = new PostMethod("/simcard.php");

        NameValuePair simcard = new NameValuePair("simcard","1330227");

        post.setRequestBody(new NameValuePair[] { simcard});

        return post;

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值