发送GET或POST请求
来源:互联网 发布:mac网速太慢怎么提网速 编辑:程序博客网 时间:2024/04/30 03:29
发送GET请求
/** * * @Title: sendGETRequest * @Description: TODO(这里用一句话描述这个方法的作用) * @author LinDL * @Email lin_donglian@163.com * @param @param path 请求路径 * @param @param params 请求参数 * @param @param encoding 编码格式 * @param @return * @param @throws MalformedURLException * @param @throws IOException 参数 * @return boolean 返回类型 */ private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws MalformedURLException, IOException { // TODO Auto-generated method stub StringBuilder url = new StringBuilder(); url.append("?"); for (Map.Entry<String, String> entry : params.entrySet()) { url.append(entry.getKey()).append("="); url.append(URLEncoder.encode(entry.getValue(), "UTF-8")); url.append("&"); } url.deleteCharAt(url.length() - 1); HttpURLConnection conn = (HttpURLConnection) new URL(url.toString()) .openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200) { return true; } return false; }
发送POST请求
/** * * @Title: sendPostRequest * @Description: TODO(这里用一句话描述这个方法的作用) * @author LinDL * @Email lin_donglian@163.com * @param @param path 请求路径 * @param @param params 请求参数 * @param @param string 编码格式,通常为"UTF-8" * @param @return * @param @throws MalformedURLException * @param @throws IOException 参数 * @return boolean 返回类型 */ private static boolean sendPostRequest(String path, Map<String, String> params, String string) throws MalformedURLException, IOException { StringBuilder data = new StringBuilder(); if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { data.append(entry.getKey()).append("="); data.append(URLEncoder.encode(entry.getValue(), "UTF-8")); data.append("&"); } data.deleteCharAt(data.length() - 1); } byte[] entity = data.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 outputStream = conn.getOutputStream(); outputStream.write(entity); if (conn.getResponseCode() == 200) { return true; } return false; }
0 0
- 发送post或get请求
- 发送GET或POST请求
- httplicent发送post或get请求
- 使用httpclient发送get或post请求
- 通过HttpWebRequest分别向服务器发送GET或POST请求
- python发送GET或POST请求以便干一些趣事
- C#模拟http 发送post或get请求
- python发送GET或POST请求以便干一些趣事
- C#模拟http 发送post或get请求
- 用PHP的 sockopen方式发送POST 或GET请求
- python发送GET或POST请求以便干一些趣事
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- WP8.1发送Post或Get请求顺带文件上传
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- Android.mk详解
- 打电话功能的实现
- MySQL学习(一)
- Android二级分类列表ListView
- 【笔记】《C#大学教程》- 第12章 GUI(一)
- 发送GET或POST请求
- Android之简单本地音乐播放器
- 105. Construct Binary Tree from Preorder and Inorder Traversal | Java最短代码实现
- 替换Notes,用BPM打造新平台
- String的compareTo方法
- AsyncTask的使用
- java降级后elipse的android项目无法运行
- mysql5.7.11解压版在windows下配置
- android自定义控件之Dialog详解