package com.wujin.httpclient.a;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.client.utils.URIBuilder;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
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.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientBase {
public static void main(String[] args) throws Exception {
String scheme="http";
String host="ws.webxml.com.cn";
//int port= -1;
String path="/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//String query="mobileCode=电话号码&userID=";
//String fragment=null;
URI uri = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPath(path)
.setParameter("mobileCode", "电话号码")
.setParameter("userID", "")
.build();
//testHttpClient();
//uri("电话号码");
header2("电话号码");
}
/**1.QuickStart 第一个demo
* @throws IOException
* @throws ClientProtocolException
*/
public static void testHttpClient() throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
System.out.println(httpGet.getURI());
System.out.println(httpResponse.getStatusLine().getStatusCode());
httpResponse.close();
}
public static void testHttpClient2() throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpGet = new HttpPost("http://httpbin.org/get");
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
System.out.println(httpGet.getURI());
System.out.println(httpResponse.getStatusLine().getStatusCode());
httpResponse.close();
}
/**
* URI的使用
*/
public static void uri(String phone) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
String scheme="http";
String host="ws.webxml.com.cn";
//int port= -1;
String path="/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//String query="mobileCode=电话号码&userID=";
//String fragment=null;
//URIUtils.createURI(scheme, host, port, path, query, fragment);
URI uri = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPath(path)
.setParameter("mobileCode", phone)
.setParameter("userID", "")
.build();
HttpGet httpGet = new HttpGet(uri);
//HttpPost httpPost = new HttpPost(uri);httpPost.setEntity(URiEncodingForm..);
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
System.out.println("uri: "+httpGet.getURI());
System.out.println("statusLine: "+httpResponse.getStatusLine());
if(entity!=null && httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
InputStream in = entity.getContent();
byte[] b = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
System.out.println(new String(b,0,len));
}
}
httpResponse.close();
}
/**
* httpResponse的头部处理
*/
public static void header(String phone) throws Exception{
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie",
"c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie",
"c2=b; path=\"/\", c3=c; domain=\"localhost\"");
Header h1 = response.getFirstHeader("Set-Cookie");
System.out.println(h1);
Header h2 = response.getLastHeader("Set-Cookie");
System.out.println(h2);
Header[] hs = response.getHeaders("Set-Cookie");
System.out.println(hs.length);
}
public static void header2(String phone) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
String scheme="http";
String host="ws.webxml.com.cn";
//int port= -1;
String path="/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//String query="mobileCode=电话号码&userID=";
//String fragment=null;
//URIUtils.createURI(scheme, host, port, path, query, fragment);
URI uri = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPath(path)
.setParameter("mobileCode", phone)
.setParameter("userID", "")
.build();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
//StringEntity se = new StringEntity(string, contentType)
HttpEntity entity = httpResponse.getEntity();
System.out.println(entity.getContentType());
System.out.println(entity.getContentLength());
System.out.println(entity.getContentEncoding());
//将内容放置到缓冲区,避免失效
entity = new BufferedHttpEntity(entity);
System.out.println(EntityUtils.toString(entity));
System.out.println(EntityUtils.toByteArray(entity).length);
//EntityUtils.consume(entity);
//这是请求体
System.out.println(entity.getContent().toString());
//消耗流中的任意可用内容
//entity.consumeContent();
//或者中断请求
httpGet.abort();
}
/**
* entity的使用:StringEntity, ByteArrayEntity, InputStreamEntity, and FileEntity.
*/
public static void entity(URI uri) throws Exception{
File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file,
ContentType.create("text/plain", "UTF-8"));
HttpPost httppost = new HttpPost(uri);
httppost.setEntity(entity);
}
public static void entity1(URI uri) throws Exception{
StringEntity entity = new StringEntity("important message",
ContentType.create("plain/text", Consts.UTF_8));
//设置 HttpEntity#setChunked()方法为 true 是通知 HttpClient 分块编码的首选最适合的编码转换
entity.setChunked(true);
HttpPost httppost = new HttpPost(uri);
httppost.setEntity(entity);
}
public static void entity2(URI uri) throws Exception{
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost(uri);
httppost.setEntity(entity);
}
/**
* 动态内容实体
* @param uri
* @throws Exception
*/
public static void entity3(URI uri) throws Exception{
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write("");
writer.write(" ");
writer.write(" important stuff");
writer.write(" ");
writer.write(" ");
writer.flush();
}
};
}
}