javahttp请求(一)----工具
httpè??æ?‚(ä??) ---- å??å…?
2 import java.io.ByteArrayOutputStream;
3 import java.io.DataOutputStream;
4 import java.io.InputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7 import java.net.URLEncoder;
8 import java.util.Map;
9 import android.util.Log;
10
11 public class NetUtil {
12
13 private static final String TAG = NetUtil ;
14 private static final int RESPONSE_OK = 200 ;
15
16 public static InputStream sendPostRequest(String urlPath,
17 Map String, String params, String encoding) throws Exception {
18 // String param = method=save id=24 name=
19 // + URLEncoder.encode( å??圆 , UTF-8 );
20 StringBuilder sb = new StringBuilder();
21 for (Map.Entry String, String entry : params.entrySet()) {
22 sb.append(entry.getKey()).append( = )
23 .append(URLEncoder.encode(entry.getValue(), encoding))
24 .append( );
25 }
26 sb.deleteCharAt(sb.lastIndexOf( ));
27 byte [] data = sb.toString().getBytes();
28 URL url = new URL(urlPath);
29 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
30 conn.setRequestMethod( POST );
31 conn.setReadTimeout( 5 * 1000 );
32 conn.setDoOutput( true ); // å??é??POSTè??æ?‚ï?Œ å?…é??è??ç??å…?è??è??出
33 conn.setUseCaches( false );
34 conn.setRequestProperty( Connection , Keep-Alive ); // ç??æŒ?é•?é??æ??
35 conn.setRequestProperty( Charset , UTF-8 );
36 //
è??ç??è??å…?å?‚æ•?çš„å??å??ï?ŒæŠŠå?‚æ•?è??åŒ?ä?ºå,?节æ•?ç?„
37 conn.setRequestProperty( Content-Length , String.valueOf(data.length));
38 // è??ç??æ•?æ??ç??å?‹
39 conn.setRequestProperty( Content-Type ,
40 application/x-www-form-urlencoded );
41
42 DataOutputStream outStream = new DataOutputStream(
43 conn.getOutputStream());
44 outStream.write(data);
45 outStream.flush();
46 outStream.close();
47 if (conn.getResponseCode() == RESPONSE_OK) {
48 return conn.getInputStream();
49 }
50 return null ;
51 }
52
53 /*
54 * å??åˆ?httpè??å›?çš„è??å…?æµ?ï?Œå??ä??è??åŒ?æˆ String
55 */
56 public static String getTextContent(String urlPath, String encoding)
57 throws Exception {
58 URL url = new URL(urlPath);
59 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
60 conn.setRequestMethod( GET );
61 conn.setReadTimeout( 5 * 1000 );
62 if (conn.getResponseCode() == RESPONSE_OK) {
63 InputStream inStream = conn.getInputStream();
64 byte [] data = readStream(inStream);
65 System.out.println( new String(data, encoding));
66 return new String(data, encoding);
67 }
68 return null ;
69 }
70
71 // è??å ?æ•?æ ?
72 public static byte [] readStream(InputStream inStream) throws Exception {
73 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
74 byte [] buffer = new byte [ 2048 ];
75 int length = - 1 ;
76 while ((length = (inStream.read(buffer))) != - 1 )
{
77 outStream.write(buffer, 0 , length);
78 }
79 outStream.close();
80 return outStream.toByteArray();
81 }
82
83 // ç›?æ??è??å›?httpå??åˆ?çš„è??å…?浕
84 public static InputStream getStreamContent(String urlPath, String encoding)
85 throws Exception {
86 InputStream inStream = null ;
87 URL url = new URL(urlPath);
88 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
89 conn.setRequestMethod( GET );
90 conn.setReadTimeout( 5 * 1000 );
91 if (conn.getResponseCode() == RESPONSE_OK) {
92 inStream = conn.getInputStream();
93 }
94 return inStream;
95 }
96
97 public static void print(String tag, String msg) {
98 Log.d(tag, msg);
99 }
100 }
101
本文档为【javahttp请求(一)----工具】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。