两种访问接口的方式(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

转载于:https://www.cnblogs.com/yangkai-cn/p/4016560.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值