您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection方式、HttpClient方式发送get、post数据给服务端

2013-12-23 17:20 525 查看
public class NewsManageService {
/**
* 保存数据
* @param title 标题
* @param timelength 时长
*/
public static boolean save(String title, String timelength) throws Exception{
String path = "http://192.168.1.100:8080/videonews/ManageServlet";
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("timelength", timelength);
return sendHttpClientPOSTRequest(path, params, "UTF-8");
}
/**
* 采用HttpClient发送POST请求
* @param path 请求路径
* @param params 请求参数
* @throws Exception
*/
private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
List<NameValuePair> param = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, 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;
}
/**
* 发送POST请求
* @param path 请求路径
* @param params 请求参数
* @throws Exception
*/
private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
StringBuilder sb = new StringBuilder();
// title=kkkkk&timelength=50
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[] entity = sb.toString().getBytes();
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允许对外输出数据
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode() == 200){
return true;
}
return false;
}

/**
* 发送GET请求
* @param path 请求路径
* @param params 请求参数
* @throws Exception
*/
private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception{
// http://192.168.1.100:8080/videonews/ManageServlet?title=xxx&timelength=30 StringBuilder sb = new StringBuilder(path);
sb.append('?');
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);
HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
return true;
}
return false;
}

public static void save(String title, String timelength, File file) throws Exception{
String path = "http://192.168.1.100:8080/videonews/ManageServlet";
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("timelength", timelength);
FormFile formFile = new FormFile(file, "videofile", "audio/mpeg");
SocketHttpRequester.post(path, params, formFile);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐