import java.io.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
* @Author: 发送 post get 请求
* @Date: 2022/3/2 13:19
*/
public class HttpClientHelper {
public static String sendPost(JSONObject jsonParam, String urls) {
StringBuffer sb=new StringBuffer();
try {
;
// 创建url资源
URL url = new URL(urls);
// 建立http连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置允许输出
conn.setDoOutput(true);
// 设置允许输入
conn.setDoInput(true);
// 设置不用缓存
conn.setUseCaches(false);
// 设置传递方式
conn.setRequestMethod("POST");
// 设置维持长连接
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 转换为字节数组
byte[] data = (jsonParam.toString()).getBytes();
// 设置文件长度
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
// 设置文件类型:
conn.setRequestProperty("contentType", "application/json");
// 开始连接请求
conn.connect();
OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
// 写入请求的字符串
out.write((jsonParam.toString()).getBytes());
out.flush();
out.close();
System.out.println(conn.getResponseCode());
// 请求返回的状态
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
System.out.println("连接成功");
// 请求返回的数据
InputStream in1 = conn.getInputStream();
try {
String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
while((readLine=responseReader.readLine())!=null){
sb.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sb.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
System.out.println("error++");
}
} catch (Exception e) {
}
return sb.toString();
}
public static String sendGet(String urlParam) throws HttpException, IOException {
// 创建httpClient实例对象
HttpClient httpClient = new HttpClient();
// 设置httpClient连接主机服务器超时时间:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 创建GET请求方法实例对象
GetMethod getMethod = new GetMethod(urlParam);
// 设置post请求超时时间
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
getMethod.addRequestHeader("Content-Type", "application/json");
httpClient.executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return result;
}
public static String sendPostByUpload(FileInputStream file, String urls) throws IOException {
String result = "";
try {
URL realUrl = new URL(urls);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/octet-stream");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 发送请求参数
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
// 发送内容
String content = "I love china";
// FileInputStream fis = new FileInputStream("D:\\idea\\java\\1650005311481.pdf");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = file.read(b, 0, b.length)) != -1) {
baos.write(b, 0, len);
}
byte[] buffer = baos.toByteArray();
System.out.println("二进制文件 " + buffer);
dos.write(buffer);
// flush输出流的缓冲
dos.flush();
// 定义BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);//打印输出结果
} catch (Exception e) {
System.out.println("异常," + e.getMessage());
e.printStackTrace();
}
return result;
}
}
发送http请求 特别是post请求并且参数是binary
最新推荐文章于 2024-07-12 15:51:39 发布