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); } }
相关文章推荐
- Android----用HTTP的get,post,HttpClient三种方式向web service提交文本数据
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HTTP 请求方式: GET和POST的比较当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 一个简单地应用--从客户端向服务器发送数据.post,get.httpclientget,httpclinetpost,文件上传.
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
- [Android开发]Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- 通过libcurl向服务端发送数据的2种方式:get/post
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 多种方式实现(jdk原生api实现/httpClient的实现) java 发送http(post/get)请求并携带参数(讲解超详细)
- httpclient使用post方式发送json数据,以及服务端的接收
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- http的post方式连接服务器,发送数据到服务端,并获取服务端的数据