public Integer saveTechnologicalChievements(TechnologicalChievements technologicalChievements) {
this.dbConstrains(technologicalChievements);
CloseableHttpClient coookiesClient = null;
try {
coookiesClient = HttpClientUtil.getCoookiesClient("http://aa.cn/DataService.asmx/LogIn?Name=xiaobaozi&Pwd=xbz123");
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("登录接口出错,不能请求接口获取数据");
}
//获取科技数据的总条数
String countJson = HttpClientUtil.sendLoginGet("http://aa.cn/DataService.asmx/GetDataCount?type=1",coookiesClient);
//下面计算要获取的次数
//先拆解json,得到总条数 然后得到要请求的总次数
JSONObject jsonObject = JSON.parseObject(countJson);
Integer resultCount = jsonObject.getInteger("resultData");
int times = resultCount / TechnologicalChievements.INSERT_PAGESIZE ;
if (!(resultCount % technologicalChievements.INSERT_PAGESIZE == 0)){
times += 1;
}
for (int i =1 ;i<= times; i++){
if (i > times){
break;
}
JSONObject jsonSub = new JSONObject();
JSONObject jsonParent = new JSONObject();
jsonSub.put("type",1);
long currentTimeMillis = System.currentTimeMillis();
jsonSub.put("date",currentTimeMillis);
String strSign = "date#"+currentTimeMillis+"type#1SignRequest";
String md5String = Md5.getMd5String(strSign).toUpperCase();
jsonSub.put("sign",md5String);
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("retrievalInfo", jsonSub.toString()));
formparams.add(new BasicNameValuePair("pageindex",i+""));
formparams.add(new BasicNameValuePair("pagesize",i*technologicalChievements.INSERT_PAGESIZE+""));
String jsonReuslt = "";
try {
//由于上面调用sendLoginGet 这个方法使得连接关闭了,这里需要重新获取一下
coookiesClient = HttpClientUtil.getCoookiesClient("http://aa.cn/DataService.asmx/LogIn?Name=xiaobaozi&Pwd=xbz123");
String jsonString = JSON.toJSONString(jsonParent);
formparams.add(new BasicNameValuePair("retrievalInfo", jsonSub.toString()));
formparams.add(new BasicNameValuePair("pageindex",i+""));
formparams.add(new BasicNameValuePair("pagesize",i*technologicalChievements.INSERT_PAGESIZE+""));
jsonReuslt = HttpClientUtil.sendLoginJsonPost("http://aa.cn/DataService.asmx/GetData" ,coookiesClient,formparams);
//这里开始解析json字符串,然后封装成duixiang对象进行入库操作
JSONObject object = JSON.parseObject(jsonReuslt);
System.out.println("object---:"+object);
Object resultData = object.get("resultData");
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(e.getMessage());
}
}
return technologicalChievementsWriteDao.insert(technologicalChievements);
}
package com.yixiekeji.core;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
import java.util.ResourceBundle;
/**
* http请求工具类
*
* @Filename: HttpClientUtil.java
* @Version: 1.0
* @Author: 陈万海
* @Email: [email protected]
*
*/
public class HttpClientUtil {
private ResourceBundle bundle;
//用来存储cookies信息的变量
// private static CookieStore store;
public static CloseableHttpClient getCoookiesClient(String loginUrl) throws IOException{
// 获取cookies信息
CookieStore store= new BasicCookieStore();
String result;
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
//测试逻辑
HttpGet httpget=new HttpGet(loginUrl);
CloseableHttpResponse response2 = httpclient.execute(httpget);
//打印返回值
result = EntityUtils.toString(response2.getEntity());
System.out.println(result);
//读取cookie信息
List cookielist = store.getCookies();
for(Cookie cookie: cookielist){
String name=cookie.getName();
String value=cookie.getValue();
System.out.println("cookie name =" + name);
System.out.println("Cookie name=" + value);
}
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
return client;
}
public static String sendGet(String url) {
HttpGet get = null;
CloseableHttpResponse resp = null;
CloseableHttpClient client = null;
try {
client = HttpClients.createDefault();
get = new HttpGet(url);
resp = client.execute(get);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = resp.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
return content;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (resp != null) {
resp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求json
* @param url
* @param json
* @return
* @throws Exception
*/
public static String sendJsonPost(String url, String json) throws Exception {
return sendPost(url, json, "application/x-www-form-urlencoded");
}
// public static String sendJsonPost(String url, String content) {
// // return sendPost(url, content, "application/json");
// return sendPost(url, content, "application/x-www-form-urlencoded");
// }
public static String sendPost(String url, String content, String type) {
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
try {
System.out.println(content);
client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", type);
StringEntity entity = new StringEntity(content, ContentType.create(type, "UTF-8"));
// StringEntity entity = new StringEntity(content);
post.setEntity(entity);
resp = client.execute(post);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
String str = EntityUtils.toString(resp.getEntity(), "utf-8");
return str;
}
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (client != null)
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (resp != null)
resp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String sendLoginPost(String url, String type,CloseableHttpClient client,List param) {
// CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
try {
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", type);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, "UTF-8");
post.setEntity(entity);
resp = client.execute(post);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
String str = EntityUtils.toString(resp.getEntity(), "utf-8");
return str;
}
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (client != null)
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (resp != null)
resp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String sendLoginJsonPost(String url,CloseableHttpClient coookiesClient, List param) throws Exception {
return sendLoginPost(url, "application/x-www-form-urlencoded",coookiesClient,param);
}
public static String sendLoginGet(String url, CloseableHttpClient coookiesClient) {
HttpGet get = null;
CloseableHttpResponse resp = null;
try {
get = new HttpGet(url);
resp = coookiesClient.execute(get);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = resp.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
return content;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (resp != null) {
resp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (coookiesClient != null) {
coookiesClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}