HttpClient提交表单 或访问接口

public static String getStringURLtoPost(String url,
			Map<String, Object> parms) {  
		if ("".equals(url) || parms == null || url.trim().length() <= 0
				|| parms.size() <= 0) {
			return null;
		}
		HttpClient httpClient = new HttpClient(); 
		PostMethod postMethod = new PostMethod(url); //此处为你访问的接口 或页面


          //设置编码 防止中文乱码


       httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
  
               //此处为集合里面的值!遍历map集合中的key和value!其实,这里是关键!这里就是表单数据


		NameValuePair[] data = new NameValuePair[parms.keySet().size()];
		Iterator it = parms.entrySet().iterator();
		int i = 0;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			data[i] = new NameValuePair(key.toString(), value.toString());
			i++;
		}
		postMethod.setRequestBody(data); //这里就是将值传过去(接口中)!


		try {
			httpClient.executeMethod(postMethod); //执行


			return postMethod.getResponseBodyAsString(); //得到接口中返回的数据


		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			postMethod.releaseConnection();  //关闭(销毁


                }
                 return null;
}

public static int getCheckInUrl(VideoAsset asset,String username,String text_comments){
		int Checkin_status = CHECKIN_STATUS_FAIL;
		StringBuffer buffer = new StringBuffer();
		Map<String, Object> parms = new HashMap<String, Object>();
		buffer.append("127.0.0.1:82/webservice/sign.jsp?action=add");
        //其实.这就相当于,我们的初始页面!然后,下面的数据就是table表单要提交的字段!


		
		if(asset == null){
			Checkin_status = CHECKIN_STATUS_FAIL;
		}if(asset.getAssetId()>=0){
			parms.put("assetid", asset.getAssetId());
		}if(asset.getItemId()>=0){
			parms.put("itemid", asset.getItemId());
		}if(!"".equals(asset.getAssetType().trim())&&asset.getAssetType()!=null){
			parms.put("assettype", asset.getAssetType().trim());
		}if(!"".equals(asset.getAssetCode().trim())&&asset.getAssetCode()!=null){
			parms.put("assetcode", asset.getAssetCode().trim());
		}if(!"".equals(username.trim())&&username !=null){
			parms.put("username", username.trim());
		}if(!"".equals(asset.getAssetName().trim())&&asset.getAssetName()!=null){
			parms.put("assetname", asset.getAssetName().trim());
		}if(!"".equals(text_comments.trim())&&text_comments!=null){
			parms.put("text_comments", text_comments);
		}
//将要提交的字段数据放入进去!


		
		return getFromUrltoPost(buffer.toString(), parms); 
	} 

public static int getFromUrltoPost(String url,Map<String, Object> parms){
		int Checkin_status = CHECKIN_STATUS_FAIL;
		try {
			Checkin_status = Integer.parseInt(StringUtil.getStringURLtoPost(url, parms));
			return Checkin_status;
		} catch (NumberFormatException e) {
			Checkin_status = CHECKIN_STATUS_UNKNOWNUSERNAME;
			return Checkin_status;
		}finally{
			return Checkin_status;
		}
		
	}

/* *最后要告诉大家的是!如果不懂,可以试想成,你做一个简单的登陆页面!里面有name和password哪个字段! *而假设你的访问页面是http://127.0.0.1:82/hello.jsp *你用post方式!那么你在集合里面就应该put两个,一个是name,一个是password *也就是put("name",name) ,put("password",password); 也访问的页面就是你的url(道理就相当于一个 *简易的登陆提交) */


	 

你可能感兴趣的:(HttpClient中文乱码,HttpClient自动提交)