HttpURLConnection 的get和post请求
/**
* 发出一个GET请求
* @param path 请求的路径,不带参数
* @param params 参数
* @return
* @throws Exception
*/
private static boolean sendGetRequest(String path, Map<String, String> params, String encoding) throws Exception{
// http://192.168.1.10:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90
StringBuilder pathBuilder = new StringBuilder(path);
pathBuilder.append('?');
for(Map.Entry<String, String> entry : params.entrySet()){
pathBuilder.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), encoding)).append('&');
}
pathBuilder.deleteCharAt(pathBuilder.length() - 1);
URL url = new URL(pathBuilder.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5*1000);
if(conn.getResponseCode()==200){
return true;
}
return false;
}
/**
* 发出一个POST请求
* @param path 请求的路径,不带参数
* @param params 参数
* @return
* @throws Exception
*/
private static boolean sendPostRequest(String path, Map<String, String> params, String encoding) throws Exception{
// method=save&name=liming&timelength=78
StringBuilder sb = new StringBuilder("");
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), encoding)).append('&');
}
sb.deleteCharAt(sb.length() - 1);
}
byte[] data = sb.toString().getBytes();//得到实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5*1000);
conn.setDoOutput(true);//能过post方式提交数据,必须要允许输出
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}
// HTTPS SSL COOKIE
private static boolean sendPostRequestHttpClient(String path, Map<String, String> params, String encoding) throws Exception{
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
BasicNameValuePair param = new BasicNameValuePair(entry.getKey(), entry.getValue());
paramPairs.add(param);
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramPairs, encoding);
HttpPost post = new HttpPost(path);
post.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();//浏览器
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}