关闭

HttpClient 4.3 开发学习(3)

标签: httpclient4.3
1249人阅读 评论(0) 收藏 举报
分类:

利用HttpClient提交Get Post请求,输出请求结果

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package com.yang.httpcomponent.learntest;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class QuickStart {

	public static void main(String[] args) throws Exception {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpGet httpGet = new HttpGet("http://localhost:8080");
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			try {
				System.out.println(response1.getStatusLine());
				HttpEntity entity1 = response1.getEntity();
				if (entity1 != null) {
					String result1 = EntityUtils.toString(entity1);//tostring方法读取实体内容,利用编码规则转换成String,默认规则是IS0-8859-1
					System.out.println(result1);//输出响应结果
					
					//讲读取的inputStream转成string
					// ByteArrayOutputStream out = new ByteArrayOutputStream();
					// byte[] data = new byte[1024];
					// int count = 0;
					// InputStream in = response1.getEntity().getContent();
					// while((count = in.read(data, 0 , 1024)) != -1)
					// out.write(data, 0, count);
					// String result1 = new String(out.toByteArray());
					// data = null;
					// System.out.println(result1);
				}
				EntityUtils.consume(entity1);//确保实体内容被消费,并且关闭内容流
			} finally {
				response1.close();
			}

			 HttpPost httpPost = new HttpPost("http://wiki.******.com/");
			 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			 nvps.add(new BasicNameValuePair("os_username", "******"));
			 nvps.add(new BasicNameValuePair("os_password", "******"));
			 UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nvps);
			 httpPost.setEntity(ent);
			 CloseableHttpResponse response2 = httpclient.execute(httpPost);
			 try {
			 System.out.println(response2.getStatusLine());
			 HttpEntity entity2 = response2.getEntity();
			 if (entity2 != null) {
			 String result2 = EntityUtils.toString(entity2);
			 System.out.println(result2);
			 }
			 EntityUtils.consume(entity2);
			 } finally {
			 response2.close();
			 }

			int num = 1001;
			// String name = "杨飞";
			String soapRequestData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
					+ "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns=\"http://personservice.yang.com\">"
					+ "<soap:Header/>"
					+ "<soap:Body>"
					+ "<queryPersonByNum>"
					+ "<num>"
					+ num
					+ "</num>"
					+ "</queryPersonByNum>"
					+ "</soap:Body>" + "</soap:Envelope>";
			String url = "http://localhost:8080/axis2/services/PersonService?wsdl";
			HttpPost httppost = new HttpPost(url);

			HttpEntity entity3 = new StringEntity(soapRequestData);
			httppost.setHeader("Content-Type",
					"application/soap+xml; charset=utf-8");
			httppost.setEntity(entity3);
			CloseableHttpResponse response3 = httpclient.execute(httppost);
			try {
				System.out.println(response3.getStatusLine());
				HttpEntity entity4 = response3.getEntity();
				if (entity4 != null) {
					String result3 = EntityUtils.toString(entity4);
					System.out.println(result3);
				}
				EntityUtils.consume(entity3);
				EntityUtils.consume(entity4);
			} finally {
				response3.close();
			}
		} finally {
			httpclient.close();
		}

	}

}



0
0
查看评论

由HttpClient_3 升级到 HttpClient_4.3.x

由HttpClient_3 升级到 HttpClient_4.3.x 前言 由于HttpClient 4.x.x 是从原包的分支出来独立成包的,以后原来那个包的httpclient 不会再升级,所以以后我们是用httpclient 新分支。而且 4.x 与之前的 3.1 包结构以及接...
  • N199109
  • N199109
  • 2014-04-11 17:58
  • 1325

HttpClient 4.3 开发学习(4)

HttpEntity的使用 package com.yang.httpcomponent.learntest; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache....
  • yang382197207
  • yang382197207
  • 2013-12-05 18:17
  • 852

HttpClient 4.3 开发学习(2)

HttpClient 4.3  GET和POST方法 package HttpClient4.yang; import java.util.ArrayList; import java.util.List; import java.net.URI; import org.apache...
  • yang382197207
  • yang382197207
  • 2013-10-30 13:44
  • 4911

HttpClient 4.3 开发学习(1)

HttpComponents Client是现在HttpClient的新版本,是对Commons HttpClient 3.x的继承者和替代品,目前官方强烈推荐升级到新 版本。好不容易对Commons HttpClient有一点眉目了,现在看HttpComponents Client完全就是一头雾水...
  • yang382197207
  • yang382197207
  • 2013-10-29 18:04
  • 4105

HttpClient使用详解(4.3.X以上版本)

Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟练...
  • zmx729618
  • zmx729618
  • 2016-07-01 11:38
  • 7362

HttpClient4.3 post与get请求工具类完整示例

整个工具类代码如下所示,可直接使用! import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.co...
  • lupangdelu
  • lupangdelu
  • 2016-03-29 17:42
  • 6728

httpclient4.3发送post和get实例代码

httpclient4.3发送post和get请求的工具代码。/** * @throws IOException * * @Title: httpPost * @Description: httpPost请求工具类 * @param url ...
  • Tombjp
  • Tombjp
  • 2016-05-11 17:59
  • 529

HttpClient 4.3详细教程

前言 Http协议应该是互联网中最重要的协议。持续增长的web服务、可联网的家用电器等都在继承并拓展着Http协议,向着浏览器之外的方向发展。 虽然jdk中的java.net包中提供了一些基本的方法,通过http协议来访问网络资源,但是大多数场景下,它都不够灵活和强大。HttpClien...
  • kobejayandy
  • kobejayandy
  • 2015-03-15 23:25
  • 16155

httpclient4.3 工具类

httpclient4.3工具类。。。。 package com.ruishenh.utils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntax...
  • ruishenh
  • ruishenh
  • 2014-05-29 16:58
  • 4428

httpclient4.5

从源码可以看出 ThreadSafeClientConnManager 在4.2时被PoolingClientConnectionManager PoolingClientConnectionManager在4.3时被PoolingHttpClientConnectionMana...
  • t_332741160
  • t_332741160
  • 2016-07-20 17:47
  • 2283
    个人资料
    • 访问:156561次
    • 积分:2452
    • 等级:
    • 排名:第17741名
    • 原创:91篇
    • 转载:13篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论