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

来源:互联网 发布:淘宝好评晒图怎么删掉 编辑:程序博客网 时间:2024/05/01 16:40
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=50if(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=30StringBuilder 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);}}

0 0
原创粉丝点击