两种访问接口的方式(get和post)
跨机器、跨语言的远程访问形式一共有三种:scoket发送数据包、http发送请求、rmi远程连接;
http发送请求方式;分为post和get两种方式
importjava.io.IOException; importjava.io.InputStream; import java.util.Map; importjava.util.concurrent.atomic.AtomicInteger; importorg.apache.commons.httpclient.HttpClient; importorg.apache.commons.httpclient.HttpException; importorg.apache.commons.httpclient.HttpStatus; importorg.apache.commons.httpclient.MultiThreadedHttpConnectionManager; importorg.apache.commons.httpclient.NameValuePair; importorg.apache.commons.httpclient.methods.GetMethod; importorg.apache.commons.httpclient.methods.PostMethod; importorg.apache.commons.httpclient.params.HttpConnectionManagerParams; importorg.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; publicclass SendHttpUrl { privatefinalstatic Logger logger = LoggerFactory.getLogger(SendHttpUrl.class); privatestatic HttpClient httpClient = null; privatestaticMultiThreadedHttpConnectionManager connectionManager = null;// 多线程管理器 privateintmaxThreadsTotal = 128;// 最大线程数 privateintmaxThreadsPerHost = 32; // 分配给每个客户端的最大线程数 privateintconnectionTimeout = 15000;// 连接超时时间,毫秒 privateintsoTimeout = 14000;// 读取数据超时时间,毫秒 publicvoid init() { connectionManager = newMultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = newHttpConnectionManagerParams(); params.setConnectionTimeout(connectionTimeout); params.setMaxTotalConnections(maxThreadsTotal); params.setSoTimeout(soTimeout); if (maxThreadsTotal > maxThreadsPerHost) { params.setDefaultMaxConnectionsPerHost(maxThreadsPerHost); } else { params.setDefaultMaxConnectionsPerHost(maxThreadsTotal); } connectionManager.setParams(params); httpClient = new HttpClient(connectionManager); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); } /** * get方式访问 * * @param url * @param callType * @param parmMap * @return */ private String callByGet(Stringurl, String callType, Map<String, String> parmMap) { if (logger.isDebugEnabled()){ logger.debug("in get,url:" + url); } GetMethod getMethod = new GetMethod(url); int statusCode = 0; try { statusCode = httpClient.executeMethod(getMethod); if (logger.isDebugEnabled()){ logger.debug("inget,statusCode:" + statusCode); } if (statusCode != HttpStatus.SC_OK) { // 访问失败 logger.error("in get,访问appagent失败。网络问题。statusCode:" + statusCode); returnnull; } InputStream inputStream = getMethod.getResponseBodyAsStream(); if (inputStream == null) { // 访问正常:获取到的数据为空 logger.error("in get,从appagent返回的数据为空!"); returnnull; } String rtn = IOUtils.toString(inputStream, "utf-8"); return rtn; } catch (HttpException e) { logger.error("get方式访问appagent失败!", e); returnnull; } catch (IOException e) { logger.error("get方式访问appagent失败!", e); returnnull; } finally { getMethod.releaseConnection(); } } /** * post方式访问 * * @param url * @param callType * @param parmMap * @return */ private String callByPost(Stringurl, String callType, Map<String, String> parmMap) { if (logger.isDebugEnabled()){ logger.debug("inpost,url:" + url); } PostMethod p = new PostMethod(url); if (parmMap != null) { NameValuePair[] params = newNameValuePair[parmMap.size()]; AtomicInteger atomicInteger = new AtomicInteger(0); for (Map.Entry<String, String> parm :parmMap.entrySet()) { NameValuePair parmValue = newNameValuePair(parm.getKey(), parm.getValue()); params[atomicInteger.getAndIncrement()] = parmValue; } p.setRequestBody(params); } try { int statusCode = httpClient.executeMethod(p); logger.debug("inget,statusCode:" + statusCode); if (statusCode != HttpStatus.SC_OK) { // 异常 logger.error("in post,访问appagent失败。网络问题。statusCode:" + statusCode); returnnull; } InputStream inputStream = p.getResponseBodyAsStream(); if (inputStream == null) { // 访问正常 logger.error("in post,从appagent返回的数据为空!"); returnnull; } String rtn = IOUtils.toString(inputStream, "utf-8"); return rtn; } catch (HttpException e) { logger.error("post方式访问appagent失败!", e); returnnull; } catch (IOException e) { logger.error("post方式访问appagent失败!", e); returnnull; } finally { p.releaseConnection(); } } }
转正请指明:blog.csdn.net/yangkai_hudong
两种访问接口的方式(get和post)的更多相关文章
- 三,memcached服务的两种访问方式
memcached有两种访问方式,分别是使用telnet访问和使用php访问. 1,使用telnet访问memcacehd 在命令提示行输入, (1)连接memcached指令:telnet 127. ...
- js对象的 两种访问方式
来对象访问属性有两种方式.有一个对象Obj = {"Name":"Langshen","AGE":"28"} 用点访问, ...
- Java中有两种实现多线程的方式以及两种方式之间的区别
看到一个面试题.问两种实现多线程的方法.没事去网上找了找答案. 网上流传很广的是一个网上售票系统讲解.转发过来.已经不知道原文到底是出自哪里了. Java中有两种实现多线程的方式.一是直接继承Thre ...
- OC中两种单例实现方式
OC中两种单例实现方式 写在前面 前两天探索了一下C++ 的单例,领悟深刻了许多.今天来看看OC中的单例又是怎么回事.查看相关资料,发现在OC中一般有两种实现单例的方式,一种方式是跟C++ 中类似的常 ...
- javascript两种声明函数的方式的一次深入解析
声明函数的方式 javascript有两种声明函数的方式,一个是函数表达式定义函数,也就是我们说的匿名函数方式,一个是函数语句定义函数,下面看代码: /*方式一*/ var FUNCTION_NAME ...
- 两种获取connectionString的方式
两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...
- Spring两种实现AOP的方式
有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...
- JIT(Just in time,即时编译,边运行边编译)、AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
JIT(Just in time,即时编译,边运行边编译).AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
随机推荐
- 三、CI框架之一个最简单的页面
一.在CI框架里面的controllers <?php defined('BASEPATH') OR exit('No direct script access allowed'); class ...
- 尝试用kotlin做一个app(一)
1.先添加一下anko库 依赖:implementation "org.jetbrains.anko:anko:$anko_version" 版本:ext.anko_version ...
- js去除热点的虚线框
1.一个页面有多张图片,图片的链接为热点绘制,在ie中点击会出现虚线框. <script type="text/javascript"> window.onload = ...
- 每天一点点之vue框架开发 - vue坑-This relative module was not found
94% asset optimization ERROR Failed to compile with 1 errors This relative module was not found: * . ...
- 在linux上部署多个tomcat
1.vim /etc/profile ##########first tomcat########### CATALINA_BASE=/usr/apache-tomcat--fore CATALIN ...
- SpringCloud学习之Hystrix请求熔断与服务降级(六)
我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...
- c语言中%s和%d的区别
/************************************************************************* > File Name: ptr_both. ...
- Git 学习 day01
Tips:最近的工作中需要用到版本控制工具git,所以准备开一个分类用来记录下自己学到的知识,以备以后温习 在安装完git之后需要设置用户名和用户邮箱: $ git config --global u ...
- ZJNU 2353 - UNO
大模拟,但是题目好像有些地方表述不清 根据UNO在初中曾被别人虐了很久很久的经历 猜测出了原本的题意 本题中的+2虽然有颜色,但是也可以当作原UNO游戏中的+4黑牌 即在某人出了+2后,可以出不同颜色 ...
- mysql数据库大规模数据读写并行时导致的锁表问题
问题介绍 最近在给学校做的一个项目中,有一个功能涉及到考核分数问题. 我当时一想,这个问题并不是很难,于是就直接采用了这样的方法:拿着一个表中的数据作为索引,去挨个遍历相关表中的数据,最后经过算分的过 ...