package test.shopclient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class ShopclientHttpClient {
public void checkGet(){
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://127.0.0.1:8080/ompMainTain/index.jsp");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println("Chunked?: " + entity.isChunked());
System.out.println(response.getStatusLine());
Header[] headers = response.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
//System.out.println("Response content length: " + entity.getContent());
System.out.println("----------------------------------------");
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
System.out.println(new String(out.toByteArray()));
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
public void checkPost(){
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://127.0.0.1:8080/ompMainTain/index.jsp");
//해더 셋팅 방법
//httppost.setHeader("Set-Cookie","JSESSIONID=ACAE3A1CF04B36AD27EF5F1311E61979; Path=/ompMainTain");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("A", "aValue"));
formparams.add(new BasicNameValuePair("B", "bValue"));
UrlEncodedFormEntity uentity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uentity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println("Chunked?: " + entity.isChunked());
System.out.println(response.getStatusLine());//HTTP/1.1 200 OK
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
//System.out.println("Response content length: " + entity.getContent());
System.out.println("----------------------------------------");
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
System.out.println(new String(out.toByteArray()));
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
httppost.abort();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
public void checkPost2(){
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://127.0.0.1:8080/ompMainTain/index.jsp");
//해더 셋팅 방법
//httppost.setHeader("Set-Cookie","JSESSIONID=ACAE3A1CF04B36AD27EF5F1311E61979; Path=/ompMainTain");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("A", "aValue"));
formparams.add(new BasicNameValuePair("B", "bValue"));
UrlEncodedFormEntity uentity = new UrlEncodedFormEntity(formparams, "UTF-8");
//httppost.setEntity(uentity);
StringEntity se = new StringEntity("ABCDEFGHIJK");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println("Chunked?: " + entity.isChunked());
System.out.println(response.getStatusLine());//HTTP/1.1 200 OK
int sc = response.getStatusLine().getStatusCode();
if (sc == HttpStatus.SC_OK) {
}
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
//System.out.println("Response content length: " + entity.getContent());
System.out.println("----------------------------------------");
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
System.out.println(new String(out.toByteArray()));
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
httppost.abort();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
public void checkTest(){
HttpClient httpclient = new DefaultHttpClient();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.kimjaehyun.co.kr/");
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
List cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + ((Cookie)cookies.get(i)));
}
// Consume response content
EntityUtils.consume(entity);
System.out.println("----------------------------------------");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
/**
* http://mudchobo.tistory.com/479
* http://blog.naver.com/dltjdals8110/50105576361
* http://credemol.blogspot.com/2011/03/httpclient-4x.html
* http://failure.egloos.com/1871086
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ShopclientHttpClient shc = new ShopclientHttpClient();
//shc.checkGet();
shc.checkPost2();
//shc.checkTest();
}
}