java实现通过post⽅式提交json参数操作
⽽且该⽹站只接受post请求,对提交的参数也只接受json格式,否则请求失败。
现将通过 post ⽅式提交json参数的⽅法记录如下:
import java.io.UnsupportedEncodingException;
import java.URI;
import java.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.fig.RequestConfig;
import org.apache.hods.HttpPost;
import org.apache.hods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.ity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/
**
* <p>@PostJsonParamsTest.java</p>
* @version 1.0
* @author zxk
* @Date 2018-3-3
*/
public class PostJsonParamsTest {
// 超时时间
private static final int RUN_TIME =10000;
// 爬取初始页数
private String page;
public static void main(String[] args) throws Exception {
PostJsonParamsTest crawl = new PostJsonParamsTest();
// 请求的url地址
String url ="v/Service/CreditService.asmx/searchOrgWithPage";
// 设置起始访问页码
crawl.setPage("1");
String isStop = "";
// 设置请求
HttpRequestBase request = null;
request = new HttpPost(url);
try {
/
/ 设置config
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(RUN_TIME)
.setConnectTimeout(RUN_TIME)
.setConnectionRequestTimeout(RUN_TIME)
.build();
request.setConfig(requestConfig);
// json 格式的 post 参数
String postParams ="{\"condition\":{\"qymc\":\"%%%%\",\"cydw\":\"\"},\"pageNo\":"+Page()+",\"pageSize\":100,count:2709846}";      System.out.println(postParams);
HttpEntity httpEntity = new StringEntity(postParams);
((HttpPost) request).setEntity(httpEntity);
// 添加请求头,可以绕过验证码
request.addHeader("Accept","application/json, text/javascript, */*");
request.addHeader("Accept-Encoding","gzip, deflate");
request.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
request.addHeader("Connection", "keep-alive");
request.addHeader("Host", "v");
request.addHeader("Content-Type", "application/json; charset=UTF-8");
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
uri = new URI(URLDecoder.String(), "UTF-8"));
request.setURI(uri);
while(!isStop.equals("停⽌")||isStop.equals("重跑")){
isStop = awlList(request);
if(isStop.equals("爬取")){
crawl.setPage(String.valueOf(Integer.Page())+1));
}
// if("2713".Page())) break;
if("2".Page())){
break;
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
throw new NumberFormatException("数字格式错误");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new UnsupportedEncodingException("不⽀持的编码集");
}
}
/**
replaceall()* 爬取搜索列表
* @param page
* @return
*/
private String crawlList(HttpRequestBase request){
int statusCode = 0;
// 下⾯两种⽅式都可以⽤来创建客户端连接,相当于打开了⼀个浏览器
CloseableHttpClient httpClient = ateDefault();
// HttpClient httpClient = ate().build();
HttpEntity httpEntity = null;
HttpResponse response = null;
try {
try {
response = ute(request);
} catch (Exception e){
e.printStackTrace();
return "重跑";
}
//打印状态
statusCode =StatusLine().getStatusCode();
if(statusCode!=200){
return "重跑";
}
//实体
httpEntity = Entity();
String searchListStr = String(httpEntity,"GBK").replaceAll("\\\\⽶", "⽶");      String allData = (String) JSONObject.parseObject(searchListStr).get("d");
// 字符串值中间含双引号的替换处理
String s = placeAll("\\{\"","{'")
.replaceAll("\":\"", "':'")
.replaceAll("\",\"", "','")
.replaceAll("\":", "':")
.replaceAll(",\"", ",'")
.replaceAll("\"\\}", "'}")
.replaceAll("\"", "")
.replaceAll("'", "\"")
.replaceAll("<br />", "")
.replaceAll("\t", "")
.replaceAll("\\\\", "?");
JSONObject jsonData = JSONObject.parseObject(s);
JSONArray jsonContent = JSONArray("orgList");
searchListStr = null;
allData = null;
s = null;
if (jsonContent==null || jsonContent.size()<1) {
return "重跑";
}
System.out.JSONString());
return "爬取";
} catch (Exception e) {
e.printStackTrace();
return "重跑";
} finally{
}
}
private String getPage() {
return page;
}
private void setPage(String page) {
this.page = page;
}
}
补充知识:JAVA利⽤HttpClient发送post请求,将请求数据放到body⾥
我就废话不多说了,⼤家还是直接看代码吧~
/**
* post请求,请求数据放到body⾥
* @param url  请求地址
* @param bodyData 参数
* @author wangyj
* @date 2019年4⽉20⽇
*/
public static String doPostBodyData(String url, String bodyData) throws Exception{
String result = "";
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = getHttpPost(url, null); // 请求地址
httpPost.setEntity(new StringEntity(bodyData, Encoding));
httpClient = getHttpClient();
// 得到返回的response
response = ute(httpPost);
HttpEntity entity = Entity();
result = getResult(entity, Encoding);
} catch (Exception e) {
throw e;
} finally {
// 关闭httpClient
if (null != httpClient) {
httpClient.close();
}
// 关闭response
if (null != response) {
response.close();
}
}
return result;
}
以上这篇java 实现通过 post ⽅式提交json参数操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。