總結(jié)一下java使用http發(fā)送post的方法:
1、post請求用于發(fā)送json 格式的參數(shù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/** * post請求(用于請求json格式的參數(shù)) * * @param url 地址 * @param params json格式的參數(shù) * @return */ public static string dopost(string url, string params) throws exception { closeablehttpclient httpclient = httpclients.createdefault(); httppost httppost = new httppost( url ); // 創(chuàng)建httppost httppost.setheader( "accept" , "application/json" ); httppost.setheader( "content-type" , "application/json" ); string charset = "utf-8" ; stringentity entity = new stringentity( params, charset ); httppost.setentity( entity ); closeablehttpresponse response = null ; try { response = httpclient.execute( httppost ); statusline status = response.getstatusline(); int state = status.getstatuscode(); if (state == httpstatus.sc_ok) { httpentity responseentity = response.getentity(); string jsonstring = entityutils.tostring( responseentity ); return jsonstring; } else { logger.error( "請求返回:" + state + "(" + url + ")" ); } } finally { if (response != null ) { try { response.close(); } catch (ioexception e) { e.printstacktrace(); } } try { httpclient.close(); } catch (ioexception e) { e.printstacktrace(); } } return null ; } |
2、用于發(fā)送key-value格式的參數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/** * post請求(用于key-value格式的參數(shù)) * * @param url * @param params * @return */ public static string dopost(string url, map params) { bufferedreader in = null ; try { // 定義httpclient httpclient client = new defaulthttpclient(); // 實例化http方法 httppost request = new httppost(); request.seturi( new uri( url ) ); //設置參數(shù) list<namevaluepair> nvps = new arraylist<namevaluepair>(); for (iterator iter = params.keyset().iterator(); iter.hasnext(); ) { string name = (string) iter.next(); string value = string.valueof( params.get( name ) ); nvps.add( new basicnamevaluepair( name, value ) ); //system.out.println(name +"-"+value); } request.setentity( new urlencodedformentity( nvps, http.utf_8 ) ); httpresponse response = client.execute( request ); int code = response.getstatusline().getstatuscode(); if (code == 200 ) { //請求成功 in = new bufferedreader( new inputstreamreader( response.getentity() .getcontent(), "utf-8" ) ); stringbuffer sb = new stringbuffer( "" ); string line = "" ; string nl = system.getproperty( "line.separator" ); while ((line = in.readline()) != null ) { sb.append( line + nl ); } in.close(); return sb.tostring(); } else { // system.out.println( "狀態(tài)碼:" + code ); return null ; } } catch (exception e) { e.printstacktrace(); return null ; } } |
第三,發(fā)送get請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * get請求 * * @return */ public static string doget(string url) { try { httpclient client = new defaulthttpclient(); //發(fā)送get請求 httpget request = new httpget( url ); httpresponse response = client.execute( request ); /**請求發(fā)送成功,并得到響應**/ if (response.getstatusline().getstatuscode() == httpstatus.sc_ok) { /**讀取服務器返回過來的json字符串數(shù)據(jù)**/ string strresult = entityutils.tostring( response.getentity() ); return strresult; } } catch (ioexception e) { e.printstacktrace(); } return null ; } |
以上所述是小編給大家介紹的java發(fā)送post方法詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/xzj80927/article/details/89098334