httpClient返回的数据类型,怎么弄

package com.etaoxue.api.third;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import ytx.org.apache.http.conn.ssl.SSLSocketFactory;

import com.jfinal.log.Logger;

public class Remote {
    private static Logger log = Logger.getLogger(Remote.class);
    /**
     * 以Post方法访问
     * @param url 请求地址
     * @param argsMap 携带的参数
     * @param content 内容
     * @return  String 返回结果
     * @throws Exception
     */
    public static String POSTMethod(String url,Map<String, Object> argsMap,String content) throws Exception{
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        if (MapUtils.isNotEmpty(argsMap)) {
            //设置参数
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
            httpPost.setEntity(encodedFormEntity);
        }
        if (StringUtils.isNotEmpty(content)) {
            httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * 可设置Cookie的Post方法
     * @param url POST方法请求url
     * @param argsMap 携带参数
     * @param content 内容
     * @param cookies cookies
     * @return
     * @throws Exception
     */
    public static String POSTMethodWithFiles(String url, Map<String, Object> argsMap,List<String> filePaths) throws Exception {
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        MultipartEntity multipartEntity = new MultipartEntity();
        //上传多张图片
        if (CollectionUtils.isNotEmpty(filePaths)) {
            for (String filePath: filePaths) {
                File file = new File(filePath);
                ContentBody fileCont = new FileBody(file, file.getName(), "image/jpeg", "utf-8");
                FormBodyPart formBodyPart = new FormBodyPart("media", fileCont);
                multipartEntity.addPart(formBodyPart);
            }
        }

        //构建Form表单参数
        if (MapUtils.isNotEmpty(argsMap)) {
            Set<Entry<String, Object>> entrySet = argsMap.entrySet();
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()){
                Entry<String, Object> entry = iterator.next();
                String name = entry.getKey();
                Object value = entry.getValue();
//              StringBody strBody = new StringBody(value.toString(), "utf-8");
                StringBody strBody = new StringBody(value.toString(),Charset.forName("utf-8"));
                multipartEntity.addPart(name,strBody);
            }
        }
        httpPost.setEntity(multipartEntity);
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        // 将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * 可设置Cookie的Post方法
     * @param url POST方法请求url
     * @param argsMap 携带参数
     * @param content 内容
     * @param cookies cookies
     * @return
     * @throws Exception
     */
    public static String POSTMethodWithFilesContentType(String url, Map<String, Object> argsMap,List<String[]> files) throws Exception {
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        MultipartEntity multipartEntity = new MultipartEntity();
        //上传多张图片
        if (CollectionUtils.isNotEmpty(files)) {
            for (String[] fileInfos: files) {
                String paramName = fileInfos[0];
                String contentType = fileInfos[1];
                String filePath = fileInfos[2];
                File file = new File(filePath);
                ContentBody fileCont = new FileBody(file, file.getName(),contentType, "utf-8");
                FormBodyPart formBodyPart = new FormBodyPart(paramName, fileCont);
                multipartEntity.addPart(formBodyPart);
            }
        }

        //构建Form表单参数
        if (MapUtils.isNotEmpty(argsMap)) {
            Set<Entry<String, Object>> entrySet = argsMap.entrySet();
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()){
                Entry<String, Object> entry = iterator.next();
                String name = entry.getKey();
                Object value = entry.getValue();
//              StringBody strBody = new StringBody(value.toString(), "utf-8");
                StringBody strBody = new StringBody(value.toString(),Charset.forName("utf-8"));
                multipartEntity.addPart(name,strBody);
            }
        }
        httpPost.setEntity(multipartEntity);
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        // 将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * 携带Header参数的POST方法
     * @param url
     * @param argsMap
     * @param headers
     * @param content
     * @return
     * @throws Exception
     */
    public static String POSTMethodWidthHeader(String url,Map<String, Object> argsMap,Map<String, String> headers,String content,boolean isSSL)throws Exception{
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        //是否加载Https安全证书
        if (isSSL) {
            X509TrustManager xtm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };

            try {
                SSLContext ctx = SSLContext.getInstance("TLS");
                ctx.init(null, new TrustManager[] { xtm }, null);
                SSLSocketFactory socketFactory = new SSLSocketFactory(ctx,SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
                httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443,(SchemeSocketFactory) socketFactory));
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        if (MapUtils.isNotEmpty(argsMap)) {
            //设置参数
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
            httpPost.setEntity(encodedFormEntity);
        }
        //设置Header参数
        if (MapUtils.isNotEmpty(headers)) {
            Set<Entry<String, String>> entrySet = headers.entrySet();
            Iterator<Entry<String, String>> iterator = entrySet.iterator();
            while(iterator.hasNext()){
                Entry<String, String> entry = iterator.next();
                String headerName = entry.getKey();
                String headerValue = entry.getValue();
                httpPost.setHeader(headerName, headerValue);
            }
        }
        if (StringUtils.isNotEmpty(content)) {
            httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * 以Get方法访问
     * @param url 请求地址
     * @param argsMap 请求携带参数
     * @return String
     * @throws Exception
     */
    public static String GETMethod(String url,Map<String, Object> argsMap) throws Exception{
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        //为GET请求链接构造参数
        url = formatGetParameter(url,argsMap);
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpGet.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * PUT方法
     * @param url 请求地址
     * @param argsMap 携带地址
     * @param cookies cookies
     * @param content 内容
     * @return
     * @throws Exception
     */
    public static String PUTMethod(String url,Map<String, Object> argsMap,String cookies,String content)throws Exception{
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPut httpPut = new HttpPut(url);
        //设置内容
        if (StringUtils.isNotEmpty(content)) {
            httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
        }
        //设置Cookies
        if(StringUtils.isNotEmpty(cookies)){
            httpPut.setHeader("Cookie", cookies);
            httpPut.setHeader("Accept", "application/json");
            httpPut.setHeader("Content-Type", "application/json");
        }
        //设置参数
        if (MapUtils.isNotEmpty(argsMap)) {
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
            httpPut.setEntity(encodedFormEntity);
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPut);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPut.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * PUT请求方法
     * @param url 请求地址
     * @param argsMap 携带参数
     * @param headerParam header参数
     * @param content 内容
     * @return
     * @throws Exception
     */
    public static String PUTMethod(String url,Map<String, Object> argsMap,Map<String,String> headerParam,String content)throws Exception{
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPut httpPut = new HttpPut(url);
        //设置内容
        if (StringUtils.isNotEmpty(content)) {
            httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
        }
        //设置Cookies
        if(MapUtils.isNotEmpty(headerParam)){
            Set<Entry<String, String>> entrySet = headerParam.entrySet();
            Iterator<Entry<String, String>> entryIter = entrySet.iterator();
            while(entryIter.hasNext()){
                Entry<String,String> entry = entryIter.next();
                String key = entry.getKey();
                String value = entry.getValue();
                httpPut.setHeader(key, value);
            }
        }
        //设置参数
        if (MapUtils.isNotEmpty(argsMap)) {
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
            httpPut.setEntity(encodedFormEntity);
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPut);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPut.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    public static String DELETEMethod(String url,Map<String, Object> argsMap,Map<String,String> headerParam)throws Exception{
        byte[] dataByte = null;
        url = formatGetParameter(url, argsMap);
        HttpClient httpClient = new DefaultHttpClient();
        HttpDelete httpDelete = new HttpDelete(url);
        //设置Cookies
        if(MapUtils.isNotEmpty(headerParam)){
            Set<Entry<String, String>> entrySet = headerParam.entrySet();
            Iterator<Entry<String, String>> entryIter = entrySet.iterator();
            while(entryIter.hasNext()){
                Entry<String,String> entry = entryIter.next();
                String key = entry.getKey();
                String value = entry.getValue();
                httpDelete.setHeader(key, value);
            }
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpDelete.abort();
        }
        //将字节数组转换成为字符串
        String result = bytesToString(dataByte);
        return result;
    }

    /**
     * 构造GET请求地址的参数拼接
     * @param url 地址
     * @param argsMap 参数
     * @return String
     */
    public static String formatGetParameter(String url,Map<String, Object> argsMap)throws Exception{
        if (url!=null && url.length()>0 && MapUtils.isNotEmpty(argsMap)) {
            if (!url.endsWith("?")) {
                url = url +"?";
            }
            if (argsMap!=null && !argsMap.isEmpty()) {
                Set<Entry<String, Object>> entrySet = argsMap.entrySet();
                Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                while(iterator.hasNext()){
                    Entry<String, Object> entry = iterator.next();
                    if (entry!=null) {
                        String key = entry.getKey();
                        Object value = entry.getValue();
//                      Object value = URLEncoder.encode(entry.getValue().toString(), "UTF-8");
                        url = url + key + "=" + value;
                        if (iterator.hasNext()) {
                            url = url +"&";
                        }
                    }
                }
            }
        }
        return url;
    }

    /**
     * 获取Entity中数据
     * @param httpEntity
     * @return
     * @throws Exception
     */
    public static byte[] getData(HttpEntity httpEntity) throws Exception{
        BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bufferedHttpEntity.writeTo(byteArrayOutputStream);
        byte[] responseBytes = byteArrayOutputStream.toByteArray();
        return responseBytes;
    }

    /**
     * 设置HttpPost请求参数
     * @param argsMap
     * @return BasicHttpParams
     */
    private static List<BasicNameValuePair> setHttpParams(Map<String, Object> argsMap){
        List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>();
        //设置请求参数
        if (argsMap!=null && !argsMap.isEmpty()) {
            Set<Entry<String, Object>> set = argsMap.entrySet();
            Iterator<Entry<String, Object>> iterator = set.iterator();
            while(iterator.hasNext()){
                Entry<String, Object> entry = iterator.next();
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
                nameValuePairList.add(basicNameValuePair);
            }
        }
        return nameValuePairList;
    }

    /**
     * 将字节数组转换成字符串
     * @param bytes
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String bytesToString(byte[] bytes) throws UnsupportedEncodingException{
        if (bytes!=null) {
            String returnStr = new String(bytes,"utf-8");
            returnStr = StringUtils.trim(returnStr);
            return returnStr;
        }
        return null;
    }
}

  

时间: 2024-12-23 20:42:25

httpClient返回的数据类型,怎么弄的相关文章

jQuery:length属性:是jQuery对象对应元素在document中的个数,返回值数据类型是Number

是jQuery对象对应元素在document中的个数,返回值数据类型是Number

struts2返回json数据类型

项目需要jar包 项目结构 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati

javascript的typeof返回哪些数据类型

1.返回数据类型 undefined string boolean number symbol(ES6) Object Function 2.强制类型转换 Number(参数)把任何类型转换成数值类型 parseInt(参数1,参数2)将字符串转换成整数 parseFloat()将字符串转换成浮点数字 string(参数):可以将任何类型转换成字符串 Boolean()可以将任何类型的值转换成布尔值 3.隐式类型转换 (1).四则运算 加法运算符+是双目运算符,只要其中一个是string类型,表

WebSocket api与服务器返回的数据类型判断(文件、二进制数据)

一.为什么需要 WebSocket? 初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处? 答案很简单,因为 HTTP 协议有一个缺陷:通信只能由客户端发起. 举例来说,我们想了解今天的天气,只能是客户端向服务器发出请求,服务器返回查询结果.HTTP 协议做不到服务器主动向客户端推送信息. 这种单向请求的特点,注定了如果服务器有连续的状态变化,客户端要获知就非常麻烦.我们只能使用"轮询":每隔一段时候,就发出一个

typeof 返回的数据类型

typeof 共返回6种数据格式: 1.object 2.undefined 3.string 4.number 5.boolean 6.function 特别注意 typeof [ ] 和 typeof null 返回的都是object typeof(Object)和typeof(Array)的结果是function,因为Object和Array本身就是内置函数. javascript中的数据类型: (symbol是ES6中新增的数据类型) 原文地址:https://www.cnblogs.c

Laravel 的 ORM 返回的数据类型小结

一.简介 Laravel 的数据库查询构造器提供了一个方便.流畅的接口,用来创建及运行数据库查询语句.它能用来执行应用程序中的大部分数据库操作,且能在所有被支持的数据库系统中使用. Laravel 的查询构造器使用 PDO 参数绑定,来保护你的应用程序免受 SQL 注入的攻击.在绑定传入字符串前不需要清理它们. 二.查询 1.从数据表中获取所有的数据列 1. 查询中的 get 与 all 方法返回的是集合. 示例# 你可以使用 DB facade 的 table 方法开始查询.这个 table

jsp Ajax请求(返回xml数据类型)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP page</title> <s

hibernate query.list() 返回的数据类型

在hibernate中,用hql语句查询实体类,采用list方法的返回结果为一个List,该List中封装的对象分为以下三种情况: 1.查询全部字段的情况下,如"from 实体类",list中封装的对象为实体类本身,各属性都将得到填充. String hql = " from Commodity "; Query query = session.createQuery(hql); List<Commodity> commodities = query.l

js 返回的数据类型 5类

对变量或值调用 typeof 运算符将返回下列值之一: undefined - 如果变量是 Undefined 类型的 boolean - 如果变量是 Boolean 类型的 number - 如果变量是 Number 类型的 string - 如果变量是 String 类型的 object - 如果变量是一种引用类型或 Null 类型的 typeof 运算符 typeof 运算符有一个参数,即要检查的变量或值.例如: var sTemp = "test string"; alert