通过get和post方式提交参数给web应用

数据小于2k时用get,大于时候用post,

指定实体数据内容类型

将信息打包成Map对象

public class NewsService {
	public static Boolean save(String title, String length) {
		String path = "http//192.168.1.100";
		Map<String,String> params = new HashMap<String, String>();
		params.put("title", title);
		params.put("timelength", length);
		try {
			return sendGETRequest(path,params,"UTF-8");//采用utf—8解决中文乱码问题
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;			
	}
用get方式将该信息提交给map对象,中文会乱码,将值用UTF-8编码,服务器中也编码,或者在服务器中使用过滤器

/**
	 * 发送GET请求
	 * @param path 请求路径
	 * @param params 请求参数
	 * @return 请求是否成功
	 * @throws Exception
	 */
	private static Boolean sendGETRequest(String path,Map<String, String> params,String encoding) //异常抛到上一层捕获处理
		throws Exception{
		//http://192.168.1.100?title=xxxx&timelength=90
		StringBuilder url = new StringBuilder(path);
		url.append("?");
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry: params.entrySet()){
				url.append(entry.getKey()).append("=");
				url.append(URLEncoder.encode(entry.getValue(),encoding));//解决中文乱码问题
				url.append("&");
			}
		<span style="font-family: Arial, Helvetica, sans-serif;">url.deleteCharAt(url.length()-1);</span>


		}	
		
		HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() ==200){
			return true;
		}
		return false;
	}

使用post方式上传参数,下图是网页提交参数给TomCat服务器的参数,左侧是客户端提交的http协议,右侧是服务器返回的响应码

private static Boolean sendPOSTRequest(String path,Map<String, String> params,String encoding) //异常抛到上一层捕获处理
			throws Exception{
			//http://192.168.1.100?title=xxxx&timelength=90 路径写为此格式
			StringBuilder url = new StringBuilder(path);
			url.append("?");
			if(params!=null && !params.isEmpty()){
				for(Map.Entry<String, String> entry: params.entrySet()){
					url.append(entry.getKey()).append("=");
					url.append(URLEncoder.encode(entry.getValue(),encoding));//解决中文乱码问题
					url.append("&");
				}	
				url.deleteCharAt(url.length()-1);
			}	
			byte[] entity = path.toString().getBytes(); //生成实体数据
			HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);                       //允许对外输出数据
			conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length",String.valueOf(entity.length));
			OutputStream os = conn.getOutputStream();
			os.write(entity);
			if(conn.getResponseCode() ==200){
				return true;
			}
			return false;
		}
使用HttpClient提交参数,不需要额外jar文件,该开源项目已经嵌到android中。client也可以完成浏览器的部分功能

private boolean sendHttpClientPOSTRequest(String path,Map<String,String> params,String encoding) throws Exception{
		List<NameValuePair> pairs = new ArrayList<NameValuePair>(); //存放请求参数
		if(params !=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry: params.entrySet()){
				pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
			}}
			// title=liming&timelength=90
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,encoding);
			HttpPost httpPost=new HttpPost(path);
			httpPost.setEntity(entity);
			DefaultHttpClient client = new DefaultHttpClient();
			client.execute(httpPost);
			HttpResponse response = client.execute(httpPost);
			if(response.getStatusLine().getStatusCode() ==200){
				return true;
			}	
		return false;
		}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值