一、有关GET和POST请求的注释
1.1 有关 GET 请求的其他一些注释:
GET 请求可被缓存 GET 请求保留在浏览器历史记录中 GET 请求可被收藏为书签 GET 请求不应在处理敏感数据时使用 GET 请求有长度限制 GET 请求只应当用于取回数据(不修改) 示例:百度api一般是get请求,除了请求地址,还有参数,?分割地址和参数: https://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation
1.2 有关 POST 请求的其他一些注释:
POST 请求不会被缓存 POST 请求不会保留在浏览器历史记录中 POST 不能被收藏为书签 POST 请求对数据长度没有要求
二、JAVA实现GET和POST请求
2.1 GET请求
public static String loadGET ( String url, String param) {
StringBuilder json = new StringBuilder ( ) ;
BufferedReader in = null ;
try {
String getUrl = url + "?" + param;
URL requestUrl = new URL ( getUrl) ;
HttpURLConnection connection = ( HttpURLConnection ) requestUrl. openConnection ( ) ;
connection. setRequestMethod ( "GET" ) ;
connection. setRequestProperty ( "accept" , "*/*" ) ;
connection. setRequestProperty ( "connection" , "Keep-Alive" ) ;
connection. setRequestProperty ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" ) ;
connection. setRequestProperty ( "Content-Type" , "application/json;charset=utf-8" ) ;
connection. connect ( ) ;
in = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) ) ) ;
String inputLine = null ;
while ( ( inputLine = in. readLine ( ) ) != null ) {
json. append ( inputLine) ;
}
} catch ( MalformedURLException e) {
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject. put ( "status" , "401" ) ;
e. printStackTrace ( ) ;
return jsonObject. toJSONString ( ) ;
} catch ( ProtocolException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( in!= null ) {
try {
in. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
return json. toString ( ) ;
}
2.2 POST请求
public static String loadPOST ( String url, String param) {
StringBuilder json = new StringBuilder ( ) ;
OutputStreamWriter out = null ;
BufferedReader in = null ;
try {
URL requestUrl = new URL ( url) ;
HttpURLConnection connection = ( HttpURLConnection ) requestUrl. openConnection ( ) ;
connection. setRequestMethod ( "POST" ) ;
connection. setRequestProperty ( "accept" , "*/*" ) ;
connection. setRequestProperty ( "connection" , "Keep-Alive" ) ;
connection. setRequestProperty ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" ) ;
connection. setRequestProperty ( "Content-Type" , "application/json;charset=utf-8" ) ;
connection. setDoOutput ( true ) ;
connection. setDoInput ( true ) ;
out = new OutputStreamWriter ( connection. getOutputStream ( ) , "utf-8" ) ;
out. write ( param) ;
out. flush ( ) ;
in = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) ) ) ;
String inputLine = null ;
while ( ( inputLine = in. readLine ( ) ) != null ) {
json. append ( inputLine) ;
}
connection. disconnect ( ) ;
} catch ( MalformedURLException e) {
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject. put ( "status" , "401" ) ;
e. printStackTrace ( ) ;
return jsonObject. toJSONString ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( out!= null ) {
out. close ( ) ;
}
if ( in!= null ) {
in. close ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
return json. toString ( ) ;
}