HttpClient Post请求两种方法。

第一种  不用传参数:

public String getAdminPwd() {
		String path = "http://10.10.10.20:85/WebSrv/GetPwd";
		//先通过地址获取流
		InputStream is = NetUtils.getInputStreamByURI(path);
		String pwd = null;
		try {
			//在通过流来获取json字符串
			String info = NetUtils.getStringByInputStream(is);
			//解析json
			JSONObject jo = new JSONObject(info);
			if (jo != null) {
				String retcode = jo.getString("retcode");
				if (retcode.equals("0")) {
					String retmsg = jo.getString("retmsg");
					pwd = retmsg;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pwd;
	}

// 通过Uri获取流
	public static InputStream getInputStreamByURI(String path) {
		// 创建HttpClient对象
		HttpClient client = new DefaultHttpClient();
		// 创建HttpPost请求方法
		HttpPost request = new HttpPost();
		// 设置URI地址
		try {
			request.setURI(new URI(path));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		HttpResponse response = null;
		try {
			response = client.execute(request);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		InputStream is = null;
		try {
			is = response.getEntity().getContent();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;
	}

// 通过流获取字符串
	public static String getStringByInputStream(InputStream is)
			throws UnsupportedEncodingException {
		StringBuilder sb = new StringBuilder();
		BufferedReader br = new BufferedReader(new InputStreamReader(is,
				"UTF-8"));
		String temp = null;
		try {
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

第二种   需要传参:

// 登陆/退出上传日志
	public boolean upLoadLog(String str) {
		String path = "http://10.10.10.20:85/WebSrv/WriteLog";
		// InputStream is = NetUtils.getInputStreamByURI(path);
		// String info = NetUtils.getStringByInputStream(is);
		boolean isOk = false;
		//设置参数 可设置多个
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("data", str));
		try {
			//得到json字符串
			String info = NetUtils.doPost(params, path);
			JSONObject jo = new JSONObject(info);
			if (jo != null) {
				String retcode = jo.getString("retcode");
				if (retcode.equals("0")) {
					isOk = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isOk;
	}

// 通过Uri获取json字符串
	public static String doPost(List<NameValuePair> params, String url)
			throws Exception {
		String result = null;
		// 获取HttpClient对象
		HttpClient httpClient = new DefaultHttpClient();
		// 新建HttpPost对象
		HttpPost httpPost = new HttpPost(url);
		if (params != null) {
			// 设置字符集
			HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
			// 设置参数实体
			httpPost.setEntity(entity);
		}

		/*
		 * // 连接超时 httpClient.getParams().setParameter(
		 * CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); // 请求超时
		 * httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
		 * 3000);
		 */
		// 获取HttpResponse实例
		HttpResponse httpResp = httpClient.execute(httpPost);
		// 获取返回的数据
		result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");

		return result;
	}

以上就是本人所用过的Http post请求方式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值