最近项目里要用到接口,只要能够post数据进行接收便可以,于是用到httpclient,而httpclient4.x与3.x还是有挺大的差别的。
public class xmccHttpPostImp {
/**
* 论文提交接口
* @param token
* @param oem_id
* @param thesis_id
* @param title
* @param author
* @param content
* @param spam
*/
public void getSubmitHttp(String token, String oem_id, String thesis_id, String title, String author, String content, String spam){
DefaultHttpClient httpclient = new DefaultHttpClient();
// URL访问地址
//String url = "Http://check.xxx.com/thesis/submit";
String url = "http://localhost:8080/xmcc_Check/servlet/SubmitServlet";
HttpPost httppost = new HttpPost(url);
try {
// 把参数值放入到PostMethod对象中
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("token", token));
nameValuePair.add(new BasicNameValuePair("oem_id", oem_id));
nameValuePair.add(new BasicNameValuePair("thesis_id", thesis_id));
nameValuePair.add(new BasicNameValuePair("title", title));
nameValuePair.add(new BasicNameValuePair("author", author));
nameValuePair.add(new BasicNameValuePair("content", content));
nameValuePair.add(new BasicNameValuePair("spam", spam));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
// 得到response对象
HttpResponse response = httpclient.execute(httppost);
// 返回码
int resStatu = response.getStatusLine().getStatusCode();
//状态位200,请求成功
if (resStatu == HttpStatus.SC_OK) {
//System.out.println("发送成功!"); //此处不能同时运行两个操作
// 获得相应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// ensure the connection gets released to the manager
EntityUtils.consume(entity);
}
}
} catch (Exception e) {
System.out.println("访问【" + url + "】出现异常!");
e.printStackTrace();
httppost.abort();
} finally {
httppost.releaseConnection();
}
}
对应的可用servlet来进行接收,然后存入数据库里
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 论文ID
String thesis_id = null;
// 标题
String title = null;
// 作者
String author = null;
// 内容
String content = null;
// 加密串
String token = request.getParameter("token");
// OEM客户
String oem_id = request.getParameter("oem_id");
// 发送方的Timestamp
String spam = request.getParameter("spam");
// 把spam从string转换成long来比较
long yourTimestamp = 0;
if(!("".equals(spam))){
yourTimestamp = Long.parseLong(spam);
}
try{
PrintWriter out = response.getWriter(); // 返回值
// 验证来源合法性
boolean isLegal = new IsLegal().isLegal(token, oem_id, spam);
if(isLegal == true){
thesis_id = request.getParameter("thesis_id"); // 论文ID
title = request.getParameter("title"); // 标题
author = request.getParameter("author"); // 作者
content = request.getParameter("content"); // 内容
// 将接收到的数据存入数据库
new CheckDao().submitInsert(token,oem_id,thesis_id,title,author,content,yourTimestamp);
// 返回值200
// out.print(200);
}else{
System.out.println("该post请求非法!");
// 返回值500
out.print(500);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("接口一已经接收到数据了!");
System.out.println("token->"+token);
System.out.println("oem_id->"+oem_id);
System.out.println("thesis_id->"+thesis_id);
System.out.println("title->"+title);
System.out.println("author->"+author);
System.out.println("content->"+content);
System.out.println("spam->"+spam);
}