java 调用http接口两种方式

HttpClient.jar下载地址 https://download.csdn.net/download/xxx_live_anyd/10340619

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

/**
*
* @author 金讯通12345接口调用
*
*/
public class HttpKxtUtils {

/**
 * HttpClient 处理方式
 * @param content  发送请求的内容  字符类型  TokenKey=cec084dfd89a0efe2bf742f7c98f7e8625547460&Name=anyd&Telephone=13594866323&Title=5554545&Content=ddd&Source=2&DataID=1111111111&TypeIndex=0&ImageList=2565
 * @param url    请求地址
 * @return
 * @author anyd
 */
public static String httpClientDoPost(String url,String content){
    try {
        // 将参数转为二进制流
        byte[] requestBytes = content.getBytes("utf-8");
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
         // 设置请求头  Content-Type
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
                requestBytes.length);
        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
                requestBytes.length, "application/json; charset=utf-8"); // 请求体
        postMethod.setRequestEntity(requestEntity);
        httpClient.executeMethod(postMethod);// 执行请求
        InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 获取返回的流
        byte[] datas = null;
        try {
            datas = readInputStream(soapResponseStream);// 从输入流中读取数据
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = new String(datas, "UTF-8");// 将二进制流转为String
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return null;
}


/**
 * 从输入流中读取数据
 * 
 * @param inStream
 * @return
 * @throws Exception
 */
public static byte[] readInputStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;
}

/**
 * HttpURLconnection  处理方式
 * @param urlPath
 * @param content
 * @return
 */
public static String httpURLDoPost(String urlPath,String content){
   try {
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");//设置本次请求的方式
        conn.setReadTimeout(5000);//设置请求时间
        conn.setDoInput(true);//是否打开输入流  默认为 true
        conn.setDoOutput(true);//是否打开输出流  默认为false
         //设置请求的内容类型
        conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        conn.connect();//表示连接
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
        writer.write(content);
        writer.close();
        int code = conn.getResponseCode();
        String result = "";
        if(code ==HttpURLConnection.HTTP_OK ){
            InputStream inputStream = conn.getInputStream();
            byte[] datas = readInputStream(inputStream);// 从输入流中读取数据
            result = new String(datas, "UTF-8");// 将二进制流转为String
        }else{
            System.out.println(urlPath+"连接失败");
            result = urlPath+"连接失败";
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
   return null;
}

}

你可能感兴趣的:(java)