测试飞信免费发送接口API的测试(HTTPClient实现)

使用优点:快捷,方便

使用缺点:用户的各种信息以明文形式在网络中传输不安全.

  仅仅用于测试

package cn.com.vnvtrip.fection.sms.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 读取fection 的配置信息
 * 
 * @author longgangbai
 * 
 */
public class Env {

private static Logger logger = Logger.getLogger(Env.class.getName());
 private static Properties props = null;
 private static Env env = null;

static {
  // 获取classpath路径配置悉尼性
  File file = new File(Env.class
    .getResource("/fectionService.properties").getFile());

if (file.exists()) {
   try {
    InputStream in = new FileInputStream(file);
    props = new Properties();
    props.load(in);
   } catch (IOException e) {
    logger.log(Level.WARNING, "加载fection 的配置失败!");
   }
  } else {
   logger.log(Level.WARNING, "fection 的配置文件不存在,请检查..");
  }
 }

/**
  * 
  */
 private Env() {
 }

/**
  * 防止获取配置信息发生异步现象
  * 
  */
 public static synchronized Env getEnv() {
  if (env == null) {
   env = new Env();
  }
  return env;
 }

/**
  * 获取配置的 Properties
  * 
  * @return
  */
 public Properties getProperties() {
  return props;
 }

public static void main(String[] args) {
  System.out.println(Env.class.getResource("/fectionService.properties")
    .getFile());
 }
}

package cn.com.vnvtrip.fection.sms.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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.NameValuePair;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * 
 * @author longgangbai
 * 
 */
public class HTTPClient {
 /**
  * 
  * @param httpurl
  * @return
  */
 public static String execute(String httpurl) {
  // 构建HttpClient的实例的应用
  HttpClient httpclient = new HttpClient();
  // 创建GET方法的实例
  GetMethod getmethod = new GetMethod(httpurl);
  // 创建的POST方法的实例 PostMethod postmethod=new PostMethod(httpurl);

// 使用系统提供的默认的恢复策略
  getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());
  try {
   // 执行GET方法的请求的实例
   int statusCode = httpclient.executeMethod(getmethod);
   // 查看响应的编码的方式
   String responseEncoding = getmethod.getRequestCharSet();
   System.out.println("the response encoding is :" + responseEncoding);
   // 检测发送是否成功的
   if (statusCode != HttpStatus.SC_OK) {
    System.out.println("Method failure:"
      + getmethod.getStatusLine());
   }
   // 得到响应的消息体
   byte[] responseBody = getmethod.getResponseBody();
   return new String(responseBody);
  } catch (HttpException e) {
   System.out.println("please check you provided http address!" + e);
  } catch (IOException e) {
   // 发生网络异常信息
   e.printStackTrace();
  } finally {
   // 释放连接
   getmethod.releaseConnection();
  }
  return null;

}

/**
  * 使用HttpClient调用远程servlet
  * 
  * @param httpurl
  * @param xmlInfo
  * @param map
  * @return
  */
 @SuppressWarnings("unchecked")
 public static InputStream executeHttp(String httpurl, String xmlInfo,
   Map<String, String> map) {
  HttpClient httpclient = new HttpClient();
  // 使用Post发送消息的方法的应用
  PostMethod postmethod = new PostMethod(httpurl);
  ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(
    xmlInfo.getBytes(), "text/html; charset=UTF-8");
  InputStream inputstream = null;
  // 设置请求的实体
  postmethod.setRequestEntity(requestEntity);
  // 设置请求的格式
  try {
   // 设置消息头信息
   if (map != null) {
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
     Entry<String, String> header = (Entry<String, String>) it
       .next();
     String key = header.getKey();
     String value = header.getValue();
     postmethod.setRequestHeader(key, value);
    }
   }
   // 发送消息的方法的
   httpclient.executeMethod(postmethod);

// 发送成功接受请求的信息
   if (postmethod.getStatusCode() == HttpStatus.SC_OK) {
    inputstream = postmethod.getResponseBodyAsStream();
   } else {
    System.out.println("unexpected failure:"
      + postmethod.getStatusLine());
   }

} catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   postmethod.releaseConnection();
  }
  return inputstream;
 }

/**
  * 使用HttpClient调用远程servlet
  * 
  * @param httpurl
  * @param xmlInfo
  * @param map
  * @return
  */
 @SuppressWarnings("unchecked")
 public static InputStream executeHttp(String httpurl,
   Map<String, String> paramMaps, Map<String, String> map) {
  HttpClient httpclient = new HttpClient();
  // 使用Post发送消息的方法的应用
  PostMethod postmethod = new PostMethod(httpurl);
  InputStream inputstream = null;
  // 设置请求的填入各个表单域的值
  List<NameValuePair> paramList = new ArrayList<NameValuePair>();
  NameValuePair[] params = new NameValuePair[paramMaps.size()];
  if (paramMaps != null) {
   for (Iterator it = paramMaps.entrySet().iterator(); it.hasNext();) {
    Entry<String, String> header = (Entry<String, String>) it
      .next();
    String key = header.getKey();
    String value = header.getValue();
    NameValuePair param = new NameValuePair();
    param.setName(key);
    param.setValue(value);
    paramList.add(param);
   }
  }
  paramList.toArray(params);
  postmethod.setRequestBody(params);
  // 设置请求的格式
  try {
   if (map != null) {
    // 设置消息头信息
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
     Entry<String, String> header = (Entry<String, String>) it
       .next();
     String key = header.getKey();
     String value = header.getValue();
     postmethod.setRequestHeader(key, value);
    }
   }
   // 发送消息的方法的
   int statusCode = httpclient.executeMethod(postmethod);
   // 自动转向的方式的应用
   // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发301或者302
   if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
     || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
    Header locationHeader = postmethod.getRequestHeader("location");
    String location = null;
    if (locationHeader != null) {
     location = locationHeader.getValue();
     System.out
       .println("the page was redirected to:" + location);

} else {
     System.out.println("Location field value is null!");
    }
   }
   // 发送成功接受请求的信息
   if (postmethod.getStatusCode() == HttpStatus.SC_OK) {
    inputstream = postmethod.getResponseBodyAsStream();
   } else {
    System.out.println("unexpected failure:"
      + postmethod.getStatusLine());
   }
  } catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   postmethod.releaseConnection();
  }
  return inputstream;
 }
}
package cn.com.vnvtrip.fection.sms.service;

import java.io.InputStream;
import java.util.Map;

import cn.com.vnvtrip.fection.sms.utils.HTTPClient;

public class SMSService {
 private static SMSService smsService = null;

private SMSService() {

}

public static SMSService getSmsService() {
  if (smsService == null) {
   smsService = new SMSService();
  }
  return smsService;
 }

/**
  * 
  * @param httpurl
  * @return
  */
 public String execute(String httpurl) {
  return HTTPClient.execute(httpurl);
 }

/**
  * 使用HttpClient调用远程servlet
  * 
  * @param httpurl
  * @param xmlInfo
  * @param map
  * @return
  */
 @SuppressWarnings("unchecked")
 public InputStream executeHttp(String httpurl, String xmlInfo,
   Map<String, String> map) {
  return HTTPClient.executeHttp(httpurl, xmlInfo, map);
 }

/**
  * 使用HttpClient调用远程servlet
  * 
  * @param httpurl
  * @param xmlInfo
  * @param map
  * @return
  */
 @SuppressWarnings("unchecked")
 public InputStream executeHttp(String httpurl,
   Map<String, String> paramMaps, Map<String, String> map) {
  return HTTPClient.executeHttp(httpurl, paramMaps, map);
 }
}

飞信免费发送接口API的测试 httpClient的更多相关文章

  1. 服务端调用接口API利器之HttpClient

    前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...

  2. 开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供) 【转】

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  3. 开源免费天气预报接口API以及全国全部地区代码!!(国家气象局提供)

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  4. 开源免费天气预报接口API以及全国所有地区代码[值得收藏]

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  5. (转)免费天气预报接口API以及全国所有地区代码!!

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  6. skflow 分类与回归接口API 简单测试

    skflow也即是 tf.contrib.learn, 是 TensorFlow 官方提供的另外一个对 TensorFlow 的高层封装,通过这个封装,用户可以和使用 sklearn 类似的方法使用 ...

  7. 免费的天气API测试接口

    网上几乎所有的天气接口都需要注册key,然后还各种频率限制,每天调用次数才几百次? 太坑爹了吧 一个简单的天气预报功能, 为什么要搞的这么复杂, 收什么费? 推荐一个真正免费的天气API接口, 返回j ...

  8. 飞信免费邮件api,飞信界面

    大家都知道飞信是能够免费发送短信的,可是飞信又没有官方的接口,所以无法借用移动的官方接口实现短信的免费发送,可是还是有一些破解的接口能够使用的. GET方法: 提交格式 http://66.zzuob ...

  9. 开源免费的天气预报接口API以及全国所有地区代码(国家气象局提供)

    天气预报一直是各大网站的一个基本功能,最近小编也想在网站上弄一个,得瑟一下,在网络搜索了很久,终于找到了开源免费的天气预报接口API以及全国所有地区代码(国家气象局提供),具体如下: 国家气象局提供的 ...

随机推荐

  1. 转载:简单介绍Python中的try和finally和with方法

    用 Python 做一件很平常的事情: 打开文件, 逐行读入, 最后关掉文件; 进一步的需求是, 这也许是程序中一个可选的功能, 如果有任何问题, 比如文件无法打开, 或是读取出错, 那么在函数内需要 ...

  2. 7款震撼人心的HTML5CSS3文字特效

    1.HTML5像素文字爆炸重组动画特效 今天我们要分享一款基于HTML5技术的文字像素爆炸重组动画特效,我们可以在输入框中指定任意文字,点击确定按钮后,就会将原先的文字爆炸散去,新的文字以像素点的形式 ...

  3. HDU1042 N! 大数的阶乘

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 由于数字特别大, 所以用数组来模拟. 经测试, 数组大小开50000 可过. 附AC代码, 欢迎 ...

  4. android 网络_网络图片查看器

    xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  5. ◆linux分区的加密与自动解密◆——Super孟再创辉煌

    首先制作分区的加密挂载: 分区的自动解密:

  6. asp.net2.0 国际化

    公司业务需要在国外开展了, 因此以前的系统要做多国语言了, 从网上搜集了好多资料, 最后选择了一个比较简单的方案 1.  打开vs2005, 新建网站, 首先在配置文件中添加配置: <syste ...

  7. C# 操作.ini文件

    1.声明变量 #region "声明变量" /// <summary> /// 写入INI文件 /// </summary> /// <param n ...

  8. 百度的domain命令到底有用吗?

    曾几何时,站长和seoer们在百度输入domain:xxxxxxxx的时候弹出的结果数量让多少人兴奋和失落,为什么我们对百度的domain命令如此着迷呢?因为我们都认为百度的domain命令是查询网站 ...

  9. Amazon S3 PHP Class Documentation

    API : http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation Example: http://www.phpb ...

  10. Python脚本控制的WebDriver 常用操作 <十六> 处理对话框

    下面将使用webdriver来处理一些页面跳出的对话框事件 测试用例场景 页面上弹出的对话框是自动化测试经常会遇到的一个问题.前端框架的对话框经常是div形式的,下面是一些常见的对话框操作事件: 打开 ...