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
- HttpURLConnection方式、HttpClient方式发送get、post数据给服务端
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- restful接口客户端和服务端开发,HttpURLConnection,HttpClient,post ,get方式调用
- HttpURLConnection HttpClient AsyncHttpClient 实现Post或者Get方式登陆
- HttpClient通过Post方式发送Json数据
- HttpClient通过Post方式发送Json数据
- HttpClient通过Post方式发送Json数据
- 使用HttpURLConnection采用get方式或post方式请求数据
- httpclient使用post方式发送json数据,以及服务端的接收
- httpurlconnection 和 httpclient 发送get、post请求
- java发送post和get请求(1)---HttpURLConnection方式
- HttpURLConnection POST/GET 处理方式
- post和get方式发送数据
- http 发送数据方式 post get soap
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 通过libcurl向服务端发送数据的2种方式:get/post
- MySQL #2006 - MySQL server has gone away 问题解决方法
- Java中class是如何加载到JVM中的(Class.forName("name")和ClassLoader.loadClass("name")的区别)
- 【线性规划与网络流24题 10】餐巾计划
- 用于软件包管理的21个Linux YUM命令 转载
- centos mongodb安装及简单实例
- HttpURLConnection方式、HttpClient方式发送get、post数据给服务端
- RPM安装命令总结--转载
- How can I exclude directories from grep -R?
- mybatis_SQL映射(5)_缓存
- 主机访问虚拟机中linux上的web服务
- 解决 ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)异常
- Install Redis on CentOS 6.4--转
- 解决两台centos虚拟机Telnet服务无法联机的问题
- linux主机间复制文件