apache的HttpClient应用

9 篇文章 0 订阅
3 篇文章 0 订阅

apache的HttpClient是对http客户端的模拟,可以视为代码版的浏览器。android也对HttpClient进行了再次封装(画蛇添足)。

1.HttpURLConnection应用举例

	public String sendXML(String path, String xml) throws Exception {
		StringBuilder sBuilder = new StringBuilder();
		byte[] data = xml.getBytes();
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
		conn.setRequestProperty("Content-Length", String.valueOf(data.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(data);
		outStream.flush();
		outStream.close();
		if (conn.getResponseCode() == 200) {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				sBuilder.append(line);
			}
		}
		return sBuilder.toString();
	}

2.java中HttpClient 的应用

		try {
			PostMethod httppost = new PostMethod(path);
			httppost.setRequestBody(request);
			int statusCode = httpclient.executeMethod(httppost);
			if (statusCode == 200) {
				if (mtype == RESULT_TYPE_OBJECT) {
					if (line == 0) {
						oistr = new ObjectInputStream(
								httppost.getResponseBodyAsStream());
						result = oistr.readObject();
					} else {
						inputStream = httppost.getResponseBodyAsStream();
						if (inputStream != null) {
							result = WebRequestI.readStream(inputStream);
						}
					}
				} else {
					result = httppost.getResponseBodyAsString();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (oistr != null) {
				try {
					oistr.close();
					oistr = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

3.android中HttpClient的应用

	public void run() {
		HttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPost httppost = new HttpPost(path);
			httppost.setEntity(request);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				if (mtype == RESULT_TYPE_OBJECT) {
					if (line == 0) {
						ObjectInputStream res = new ObjectInputStream(
								resEntity.getContent());
						result = res.readObject();
					} else {
						InputStream inputStream = resEntity.getContent();
						if (inputStream != null) {
							result = WebRequestI.readStream(inputStream);
						}
					}
				} else {
					result = EntityUtils.toString(resEntity);
				}
			}
			if (resEntity != null) {
				resEntity.consumeContent();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.getConnectionManager().shutdown();
			} catch (Exception ignore) {
			}
		}
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值