java模拟http请求
java模拟http发送请求,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求,
方法一:
package main.utils; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL; public class HttpUtilTest {
Log log = new Log(this.getClass());//初始化日志类
/**
* @作用 使用urlconnection
* @param url
* @param Params
* @return
* @throws IOException
*/
public String sendPost(String url,String Params)throws IOException{
OutputStreamWriter out = null;
BufferedReader reader = null;
String response="";
try {
URL httpUrl = null; //HTTP URL类 用这个类来创建连接
//创建URL
httpUrl = new URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);//设置不要缓存
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//POST请求
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(Params);
out.flush();
//读取响应
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response+=lines;
}
reader.close();
// 断开连接
conn.disconnect(); log.info(response.toString());
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(reader!=null){
reader.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
} return response;
}
}
方法二:使用httpclient实现
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import main.utils.Log; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; //post请求方法
public String sendPost(String url, String data) {
String response = null;
log.info("url: " + url);
log.info("request: " + data);
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity stringentity = new StringEntity(data,
ContentType.create("text/json", "UTF-8"));
httppost.setEntity(stringentity);
httpresponse = httpclient.execute(httppost);
response = EntityUtils
.toString(httpresponse.getEntity());
log.info("response: " + response);
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
附:httpClient 4.3中文手册,来自开源中国:https://my.oschina.net/u/565871/blog/701214
java模拟http请求的更多相关文章
- 上curl java 模拟http请求
最近,我的项目要求java模拟http请求,获得dns解决 tcp处理过的信息特定的连接. java api提供urlConnection apache提供的httpClient都不能胜任该需求,二次 ...
- curl java 模拟http请求
curl java 模拟http请求 直接上代码: public static void main(String args[]) throws Exception { String url = &qu ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- Java模拟http请求调用远程接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- Java 模拟http请求
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- java模拟http请求(代理ip)
java实现动态切换上网IP (ADSL拨号上网) java动态设置IP java模拟http的Get/Post请求 自动生成IP模拟POST访问后端程序 JAVA 动态替换代理IP并模拟POST
- Java模拟http请求远程调用接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- java模拟http请求上传文件,基于Apache的httpclient
1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...
- Java模拟POST请求发送二进制数据
在进行程序之间数据通信时我们有时候就需要自定义二进制格式,然后通过HTTP进行二进制数据交互.交互的示例代码如下: public static void main(String[] args) { S ...
随机推荐
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-22: ordinal not in range(128)
在python2.7下,将字符串写入到文件时会出现"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in p ...
- 新闻cms管理系统(二) ---- 后台登录功能
1.页面准备: (1)前端资源的导入:将准备好的页面添加到项目中,放到Public目录下(公共的页面样式.js.图片等资源) (2)添加登录的视图模板 将登录页面的视图放到Amin>View&g ...
- rabbitmq关于guest用户登录失败解决方法
刚安装完rabbitmq,登录的时候出现了: login failed问题: 查看rabbitmq的文档,发现在3.3.1以后的版中,处于安全的考虑,guest这个默认的用户只能通过localhos ...
- Linux 启动进程结束进程通用代码
linux启动springboot项目 start.sh #!/bin/sh rm -f tpid nohup java -jar restDate--SNAPSHOT.jar --spring.pr ...
- springboot项目用maven打jar包
clean package -Dmaven.test.skip=true idea eclipse STS
- PAT——1069. 微博转发抽奖
小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...
- ucosii任务堆栈的作用是什么呢?
http://blog.csdn.net/supreme42/article/details/7397241 第一,当任务运行时,它用来保存一些局部变量:第二,当任务挂起时,它负责保存任务的运行现场, ...
- 关于contentquery webpart的pdf文件如何在OOS上打开,并且所有文件在浏览器新起的页面打开?
function SetHref(pdf) { var c = pdf.href; var d = "http://eds.jd.com"; var f = "" ...
- iOS 11.4.1 正式版越狱
在 2018 年 Electra 最新能支持到 11.3.1 越狱,很长的一段时间 11.4 只能支持 Beta 版本,临近春节给了我们一个大礼物,终于支持 iOS 11.4-11.4.1,目前 iO ...
- 追溯了解Ubuntu之安装操作步骤(贰)
1.首先从官网中下载32位或64位安装程序: 2.下载安装包后不需要解压:直接双击即可:在里面可以看到wubi.exe应用程序,双击打开: 如果之前已经安装过需要卸载重新安装: 3.目标驱动器是安装的 ...