httpget请求

import java.io.IOException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.google.gson.Gson;
import com.ksyy.ksyy.bean.OperateResult;

public class HttpClientUtils {

 /**
  * 短信服务IP
  */
 private String ip = "192.168.1.2";
 /**
  * 短信服务端口
  */
 private String port = "80";
 /**
  * 短信服务客户端用户名
  */
 private String user = "zhang";
 /**
  * 短信服务客户端用户密码
  */
 private String password = "pw";
 /**
  * 短信服务客户端发送者标示
  */
 private String sender = "zlw";

 /**
  * 短信服务客户端发送内容描述
  */
 private String preContent = "短信验证码:";

 /**
  * 短信服务客户端发送验证码,过期时间(单位:分钟)
  */
 private int smsExpire =5;
 
 public static void main(String arg[]) throws Exception {
  String tel = "187";
  String content = "2";
  String[] strs = new String[] { "utf-8", tel, content };
  new HttpClientUtils().sendSMS(strs);
 }

 /**
  * <p>
  * httpClient的get请求方式
  * </p>
  *
  * @param url
  *            =
  *            "http://192.168.1.2/Apx/Send?user=zhang&password=pw&tel=124234&content=432sf&sender=3424"
  *            ;
  * @param charset
  *            ="utf-8";
  * @return
  * @throws Exception
  */
 public OperateResult sendSMS(String arg[]) throws Exception {
  /*
   * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。
   * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod
   * 生成的Get方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。
   */
  String url = "http://" + ip + ":" + port
    + "/apx/Send?user=" + this.getUser()
    + "&password=" + this.getPassword() + "&tel=" + arg[0]
    + "&content=" + URLEncoder.encode(this.getPreContent() + arg[1] ,"utf-8")+ "&sender="
    + this.getSender();
  String response = "";
  
  OperateResult bean=null;
  Gson gson=new Gson();
  
  System.out.println(url);
  if (null == url || !url.startsWith("http")) {
   bean=new OperateResult(false,"请求地址格式不对");
      return bean;
  }
  /* 1 生成 HttpClinet 对象并设置参数 */
  HttpClient httpClient = new HttpClient();
  // 设置 Http 连接超时为5秒
  httpClient.getHttpConnectionManager().getParams()
    .setConnectionTimeout(5000);
  
  /* 2 生成 GetMethod 对象并设置参数 */
  GetMethod getMethod = new GetMethod(url);
  // 设置 get 请求超时为 5 秒
  getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
  // 设置请求重试处理,用的是默认的重试处理:请求三次
  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());

  // 设置请求的编码方式
   getMethod.addRequestHeader("Content-Type",
     "application/x-www-form-urlencoded; charset=utf-8");

  /* 3 执行 HTTP GET 请求 */
  try {
   int statusCode = httpClient.executeMethod(getMethod);
   // System.out.println(statusCode);
   /* 4 判断访问的状态码 */
   if (statusCode != HttpStatus.SC_OK) {
    bean=new OperateResult(false,"发送失败,请重试!");
   }
   else{
    /* 5 处理 HTTP 响应内容 */
    // HTTP响应头部信息,这里简单打印
    Header[] headers = getMethod.getResponseHeaders();
    for (Header h : headers)
     System.out.println(h.getName() + "------------ " + h.getValue());

    // 读取 HTTP 响应内容,这里简单打印网页内容
    response = new String(getMethod.getResponseBodyAsString().getBytes(
      "gbk"));
    bean = gson.fromJson(response, OperateResult.class);
   }
  } catch (HttpException e) {
   // 发生致命的异常,可能是协议不对或者返回的内容有问题
   System.out.println("Please check your provided http address!");
   e.printStackTrace();
   bean=new OperateResult(false,"发送失败,请重试!");
  } catch (IOException e) {
   // 发生网络异常
   e.printStackTrace();
   bean=new OperateResult(false,"发送失败,请重试!");
  } finally {
   /* 6 .释放连接 */
   getMethod.releaseConnection();
  }
  return bean;
 }
 
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值