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