httpclient
来源:互联网 发布:程序员未来前景怎么样 编辑:程序博客网 时间:2024/06/14 11:25
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟练掌握HttpClient是很重要的必修内容,掌握HttpClient后,相信对于Http协议的了解会更加深入。
一、简介
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。
下载地址: http://hc.apache.org/downloads.cgi
说白了就是一个工具包,提供http的
使用方法
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6. 释放连接。无论执行方法是否成功,都必须释放连接
参见
http://blog.csdn.net/caesardadi/article/details/8621595
以上是粗略百度的,接下来具体学习httpclient
http://wenku.baidu.com/link?url=Z6fiY9pHdsWihBXeYxks__RI0rtXcIDlA2z-
YM_3A7Y364aPjODSyYxNz5kUdhioyTxNMx_TNPn0kHWYiEN6qljFg6uCKHPObxz9dJCG_-q
这个是api教程
首先创建链接 创建链接:1httpclient hc=new httpclieat();
get请求2 httpget httpget=new Httpget()括号内要求传入一个url 为请求的地址
执行get请求 3 HttpResponse response = httpclient.execute(httpget); 返回response 也就是http响应
响应获取 里面的东西 4 HttpEntity entity = response.getEntity(); 具体什么东西暂时未知
HttpEntity entity = response.getEntity();
System.out.println("entity:"+entity.toString());
JSONObject jsonObject = JSONObject.fromObject(entity.toString());
System.out.println("result:"+jsonObject.toString());
String state = jsonObject.getString("code");
jsonobject把entity里面的东西转换成jsonobject 找到code的参数
如果code参数判断是否设置成功订单
if("0".equals(state)){
//调用淘宝接口设置订单成功状态
taoBaoseller_flag(sjOrderEntity.getOrdernumber(),1,"炎信",sjOrderEntity.getCarrier(),sjOrderEntity.getTSBaseUser().getId());
}else{
//调用淘宝接口设置订单失败状态
taoBaoseller_flag(sjOrderEntity.getOrdernumber(),0,"炎信",sjOrderEntity.getCarrier(),sjOrderEntity.getTSBaseUser().getId());
//设置错误原因
sjOrderEntity.setOrdernote(jsonObject.getString("message"));
//调用结果处理方法在本地数据库中插入失败结果
DepositReturnProcess(sjOrderEntity, "0", jsonObject.toString());
}
然后有回调类
回调类是别人来调我的,获取业务的东西
具体代码如下
@Controller
@RequestMapping("/sjLiuliangRUANYUNReturnController") 首先通过springmvc扫描controller 配置调用url
public class sjLiuliangRUANYUNReturnController extends BaseController{
@Autowired 这里自动注入实现类,通过接口名字相同找到
private SystemService systemService;
@Autowired
private SjDepositInterfaceServiceI sjDepositInterfaceService; 这个类是包含所有业务的大类,里面有方法,需要别人调用的
AtuoTaoBaoSellser myTaoBao = new AtuoTaoBaoSellser();
/**
* 鍏呭€?
*
* @param ids
* @return
*/
@RequestMapping(value = "/czjg.do", method = RequestMethod.POST)
@ResponseBody
public synchronized void czjg(HttpServletRequest request, HttpServletResponse response) {
System.out.println("软云 come in");
try{
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = "" ;
StringBuffer sb = new StringBuffer("");
while((s=br.readLine())!=null){
sb.append(s);
}
// System.out.println(sb.toString());
JSONObject jsonRes = new JSONObject(sb.toString());
String transno = jsonRes.getString("transno");
String ordercode = jsonRes.getString("ordercode");
String orderid = jsonRes.getString("orderid");
// System.out.print("transno="+transno);
// System.out.print("ordercode="+ordercode);
// System.out.println("orderid="+orderid);
List<SjOrderEntity> sjOrderEntityList = systemService.findByProperty(SjOrderEntity.class, "ordernumber", transno);
if((null != sjOrderEntityList)&&(sjOrderEntityList.size() > 0)){
if((null == sjOrderEntityList.get(0).getReturnnote())||("".equals(sjOrderEntityList.get(0).getReturnnote()))){
String State = "0";
if(ordercode.equals("100")){
State = "1";
String sqlstr = "select * from sj_order where " +
"sj_order.ordernumber='" + transno + "'";
List<Map<String, Object>> sjProductEntitymapList = systemService.findForJdbc(sqlstr, null);
// System.out.println("sjProductEntitymapList="+sjProductEntitymapList);
if(sjProductEntitymapList!=null&&sjProductEntitymapList.size()>0){
String userID = sjProductEntitymapList.get(0).get("userid").toString();
String productname = sjProductEntitymapList.get(0).get("productname").toString();
String sqlflownum = "SELECT * FROM sj_product WHERE sj_product.name = '"+productname+"'";
List<Map<String, Object>> sjFlowmapList = systemService.findForJdbc(sqlflownum, null);
// System.out.println("sjFlowmapList="+sjFlowmapList);
String ordernum ="";
if(sjFlowmapList!=null&&sjFlowmapList.size()>0){
ordernum = sjFlowmapList.get(0).get("SPEC").toString();
}
String sql = "select * from sj_interface where " +
"sj_interface.userid='" + userID + "'";
List<Map<String, Object>> sjUsermapList = systemService.findForJdbc(sql, null);
// System.out.println("sjUsermapList="+sjUsermapList);
if(sjUsermapList!=null&&sjUsermapList.size()>0){
if(sjUsermapList.get(0).get("SMSMOULD")!=null&&sjUsermapList.get(0).get("SMSMOULD").toString().length()>0){
String smsmould = sjUsermapList.get(0).get("SMSMOULD").toString();
int x= (int)(Long.parseLong(ordernum)/1024.00);
if(x>=1)smsmould = smsmould.replaceAll("@", x+"G");
else
smsmould = smsmould.replaceAll("@", ordernum+"M");
myTaoBao.taoBaoSendSMS(sjOrderEntityList.get(0).getPhone(),smsmould);
}
}
}
}
String Carrier = sjOrderEntityList.get(0).getCarrier();
sjDepositInterfaceService.DepositReturnProcess(sjOrderEntityList.get(0), State, jsonRes.toString());
myTaoBao.taoBaoseller_flag(transno,State,"软云",Carrier);
myTaoBao.taoBaoSend(transno,State);
}
}
try {
PrintWriter outer = response.getWriter();
outer.print("success");//顫?
outer.flush();
outer.close();
} catch (Exception e) {
e.printStackTrace();
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- HttpClient
- HttpClient
- httpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- 回溯算法
- swift 字符串基础
- static_cast, dynamic_cast, const_cast探讨
- 2D Toolkit官方教程翻译
- web server与app server
- httpclient
- udev 自动挂载U盘文件系统指定目录
- 基于深度学习的图像识别算法研究
- SurfaceView深入认识
- jQuery 隐藏、显示、淡入淡出、滑动
- 关于vimrc文件小问题记录
- vue.js--Tab选项卡
- opengles之卡通着色算法
- Day36: Prepare for interview