HttpClient 4.3 开发学习(3)

原创 2013年12月05日 16:31:20

利用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();
		}

	}

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HttpClient 4.3 开发学习(1)

HttpComponents Client是现在HttpClient的新版本,是对Commons HttpClient 3.x的继承者和替代品,目前官方强烈推荐升级到新 版本。好不容易对Commons...

HttpClient 4.3 开发学习(2)

HttpClient 4.3  GET和POST方法 package HttpClient4.yang; import java.util.ArrayList; import java.util....

Apache HttpClient 4.3开发指南

《Apache HttpClient 4.3开发指南》 原文地址:http://blog.csdn.net/chszs/article/details/16854747 一、概述 Apache ...

Apache HttpClient 4.3开发指南(三个例子)

原文摘录:.net

《Apache HttpClient 4.3开发指南》

转来的博客主页:http://blog.csdn.net/chszs 一、概述 Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼...

Apache HttpClient 4.3开发指南

《Apache HttpClient 4.3开发指南》 作者:chszs,转载需注明。博客主页:http://blog.csdn.NET/chszs 一、概述 Apache Http...

由HttpClient_3 升级到 HttpClient_4.3.x

由HttpClient_3 升级到 HttpClient_4.3.x 前言 由于HttpClient 4.x.x 是从原包的分支出来独立成包的,以后原来那个包的httpclient 不会再升...

Android 开发学习记录(3)---- httpclient使用(二)

之前在Android 开发学习记录(2)---- httpclient使用(一)中介绍了httpl

周志华《机器学习》课后习题解答系列(五):Ch4.3 - 编程实现ID3算法

这里采用了自己编程的方式实现ID3算法,并基于西瓜数据集生成了决策树,评估了其好坏。

机器学习(周志华)习题解答4.3: Python小白详解ID3决策树的实现

我是刚刚学python,所以尽我所能十分详细地解释了几乎每个步骤……希望和我一样的新手可以从中获益。本文以周志华《机器学习》习题4.3为例,用python实现了ID3决策树的构建和绘图。
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)