httpclient进行post请求

最近项目里要用到接口,只要能够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);
	}	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值