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
    评论
Apache HttpClient 是一个 Java HTTP 客户端库,用于发送 HTTP 请求和处理响应。如果您在使用 Apache HttpClient 期间遇到异常,可以参考以下常见的例外列表: 1. `java.net.UnknownHostException`:当无法解析主机名时抛出。通常是由于 DNS 配置不正确或网络连接故障造成的。 2. `java.net.SocketTimeoutException`:当请求超时时抛出。可以通过设置连接超时和套接字超时来避免这种情况。 3. `org.apache.http.conn.HttpHostConnectException`:当无法连接到远程主机时抛出。可能是由于远程主机故障、防火墙或代理问题造成的。 4. `org.apache.http.conn.ConnectTimeoutException`:当连接超时时抛出。可以通过设置连接超时来避免这种情况。 5. `org.apache.http.client.ClientProtocolException`:当客户端尝试执行与协议不兼容的操作时抛出。通常是由于应用程序错误造成的。 6. `java.io.IOException`:当发生 I/O 错误时抛出。可能是由于网络连接故障、文件系统错误或其他原因造成的。 7. `org.apache.http.client.NonRepeatableRequestException`:当尝试重复使用不可重复请求体时抛出。通常是由于请求体已被消耗或关闭造成的。 8. `org.apache.http.client.HttpResponseException`:当收到非成功响应代码时抛出。可以通过检查响应代码和消息来确定响应类型。 9. `org.apache.http.client.ClientProtocolException`:当客户端尝试执行与协议不兼容的操作时抛出。通常是由于应用程序错误造成的。 10. `org.apache.http.client.CircularRedirectException`:当重定向链中存在循环时抛出。可以通过设置最大重定向次数来避免这种情况。 以上是一些常见的例外列表,但不是全部。如果您遇到其他异常,可以查看 Apache HttpClient 文档或在 Stack Overflow 上寻求帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值