33 */
44package cn .aofeng .demo .httpclient ;
55
6+ import java .io .File ;
67import java .io .IOException ;
78import java .net .URISyntaxException ;
9+ import java .util .ArrayList ;
10+ import java .util .List ;
811
912import org .apache .http .Header ;
1013import org .apache .http .HttpEntity ;
14+ import org .apache .http .NameValuePair ;
1115import org .apache .http .StatusLine ;
1216import org .apache .http .client .ClientProtocolException ;
17+ import org .apache .http .client .entity .UrlEncodedFormEntity ;
1318import org .apache .http .client .methods .CloseableHttpResponse ;
1419import org .apache .http .client .methods .HttpGet ;
1520import org .apache .http .client .methods .HttpPost ;
21+ import org .apache .http .entity .ContentType ;
22+ import org .apache .http .entity .FileEntity ;
1623import org .apache .http .impl .client .CloseableHttpClient ;
1724import org .apache .http .impl .client .HttpClients ;
25+ import org .apache .http .message .BasicNameValuePair ;
1826import org .apache .http .util .EntityUtils ;
1927import org .apache .log4j .Logger ;
2028
@@ -37,7 +45,31 @@ public void get() throws URISyntaxException, ClientProtocolException, IOExceptio
3745 CloseableHttpResponse response = client .execute (get );
3846 processResponse (response );
3947 }
40-
48+
49+ public void post () throws ClientProtocolException , IOException {
50+ List <NameValuePair > params = new ArrayList <NameValuePair >();
51+ params .add (new BasicNameValuePair ("chinese" , "中文" ));
52+ params .add (new BasicNameValuePair ("english" , "英文" ));
53+ UrlEncodedFormEntity entity = new UrlEncodedFormEntity (params , _charset );
54+
55+ CloseableHttpClient client = HttpClients .createDefault ();
56+ HttpPost post = new HttpPost (_targetHost +"/post" );
57+ post .addHeader ("Cookie" , "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098" );
58+ post .setEntity (entity );
59+ CloseableHttpResponse response = client .execute (post );
60+ processResponse (response );
61+ }
62+
63+ public void sendFile (String filePath ) throws UnsupportedOperationException , IOException {
64+ CloseableHttpClient client = HttpClients .createDefault ();
65+ HttpPost post = new HttpPost (_targetHost +"/file" );
66+ File file = new File (filePath );
67+ FileEntity entity = new FileEntity (file , ContentType .create (ContentType .TEXT_PLAIN .getMimeType (), _charset ));
68+ post .setEntity (entity );
69+ CloseableHttpResponse response = client .execute (post );
70+ processResponse (response );
71+ }
72+
4173 private void processResponse (CloseableHttpResponse response )
4274 throws UnsupportedOperationException , IOException {
4375 try {
@@ -65,21 +97,14 @@ private void processResponse(CloseableHttpResponse response)
6597 }
6698 }
6799
68- public void post () throws ClientProtocolException , IOException {
69- CloseableHttpClient client = HttpClients .createDefault ();
70- HttpPost post = new HttpPost (_targetHost +"/post" );
71- post .addHeader ("Cookie" , "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098" );
72- CloseableHttpResponse response = client .execute (post );
73- processResponse (response );
74- }
75-
76100 /**
77101 * @param args
78102 */
79103 public static void main (String [] args ) throws Exception {
80104 HttpClientBasic basic = new HttpClientBasic ();
81- basic .get ();
82- basic .post ();
105+ // basic.get();
106+ // basic.post();
107+ basic .sendFile ("/devdata/projects/open_source/mine/JavaTutorial/LICENSE" );
83108 }
84109
85110}
0 commit comments