java发送http的get、post请求
/** * 向指定URL发送GET方法的请求 * * @param url发送请求的URL * @param param请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection();// 打开和URL之间的连接 connection.setRequestProperty("accept", "*/*");// 设置通用的请求属性 connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect();// 建立实际的连接 Map> map = connection.getHeaderFields();// 获取所有响应头字段 for (String key : map.keySet()) {// 遍历所有的响应头字段 System.out.println(key + "--->" + map.get(key)); } in = new BufferedReader(new InputStreamReader(// 定义 BufferedReader输入流来读取URL的响应 connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }
/** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(param);xn--%20-8u3esc473lftkkz5d4uh out.flush();xn--%20flush-hz6mgxn41v6hxa2vyrg5c in = new BufferedReader(// 定义BufferedReader输入流来读取URL的响应 new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!"+e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } }
public static void main(String[] args) { //发送 GET 请求 String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456"); System.out.println(s); //发送 POST 请求 String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456"); System.out.println(sr); }
正在加载评论...
相关文章
- 配置@ConfigurationProperties后读取yml文件的参数...
- Caused by: java.lang.NoClassDefFoundError: Coul...
- springboot加入SSL证书设置https访问
- Springboot用pagehelper分页插件查询Oracle的速度...
- shiro的登录账号密码校验过程,SimpleAuthenticati...
- Shiro控制没有登录时,返回指定JSON格式数据
- SpringBoot中mybatis返回的Date时间格式化
- ClassNotFoundException: org.aspectj.lang.annota...
- org.apache.shiro.UnavailableSecurityManagerExce...
栏目列表
推荐阅读
- java的StringEscapeUtils转义与反转义
- Mybatis的foreach事列,如何获取foreach的index值
- Java Ehcache缓存的timeToIdleSeconds和timeToLiveSeconds区别
- errcode: 40001, errmsg: invalid credential, access_token is invalid or not latest rid: 5f6981c2-6cc9cb20-6833977a
- 服务器侦听端口 netstat -na 命令详解
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- tomcat启动maven工程报错-Updating status for Tomcat v7.0 Server at localhost...
- Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
- Java后台获取当前访问客户端的真实IP地址(外网或内网地址)
- com.googlecode.aviator.AviatorEvaluator规则引擎用法
- StringSubstitutor替换占位符,处理SQL参数
- java拦截器HandlerInterceptor的preHandle、postHandle与afterCompletion三个方法
- java中Session缓存和Cache缓存的区别
- Java8对象集合转List<Integer>id集合,转List<String>name集合,List<Class>对象集合
- Java导出excel利用jxls合并单元格jx:mergeCells实现表格嵌套技术
- java图片文件base64流之间的互转帮助类源码下载
- 利用ObjectMapper,将json数组转List<Map<String,Object>>
- java的DES加密解密辅助类
- Java Ecache缓存工具类源码下载
- IllegalArgumentException: Illegal base64 character 3a