/** * Http访问 * @param httpUrl String * @param parameters Map<String,String> * @return String * @throws IOException */ public static synchronized String httpClient(String httpUrl,Map<String,String> parameters) throws IOException { URL url = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); StringBuffer result = new StringBuffer(); InputStreamReader reader = null; BufferedReader bufReader = null; try { StringBuffer params = new StringBuffer(); if (parameters != null && parameters.size() > 0) { conn.setDoOutput(true); for (Map.Entry<String, String> parameter:parameters.entrySet()) { String key = parameter.getKey(); String value = parameter.getValue(); params.append("&" + key + "=" + value); } params.substring(1); OutputStream output = conn.getOutputStream(); output.write(params.toString().getBytes()); } InputStream input = conn.getInputStream(); reader = new InputStreamReader(input); bufReader = new BufferedReader(reader); while (true) { String line = bufReader.readLine(); if (line == null) { break; } result.append(line); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (bufReader != null) { bufReader.close(); } if (reader != null) { reader.close(); } } return result.toString(); }
相关推荐
web端就是一个简单的php页面,已经放在的我的sae albet.sinaapp.com上,直接使用demo中的url就行,当然,也可以把testPost.PHP放在自己的网站,把url改了就行
HttpUrlConnection对象模拟POST带JSON字符串的AJAX请求失败! 我的目的是抓取响应结果字符串"301",但抓取的结果是一堆HTML代码,哪位大神看看哪有毛病?
Java原生HttpUrlConnection访问WebService实例,附相关jar包...新手整理不喜勿喷
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
小编刚刚用HttpURLConnection做了一个android的APP,不小心踩到了几个坑,总结下最常用的就通过HttpURLConnection来POST提交JSON数据与GET请求JSON数据。此外就是下载图片,下载图片分为显示进度与不显示进度两种。...
HttpUrlConnection 用Post方式提交请求 ,我找的不容易,需要的时候很重要
Java利用HttpURLConnection发送post请求上传文件
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
httpurlconnection, http post, http get
介绍了Android 中HttpURLConnection与HttpClient使用的简单实例,有需要的朋友可以参考一下
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
Android HttpUrlConnection使用方法 post and get json.
//使用POST提交 hc.addRequestProperty("Cookie", getSessionId()); hc.setDoOutput(true); hc.connect(); OutputStream out = hc.getOutputStream(); //参数是a=""&b=""这样拼接的一个串 out....
主要给大家介绍java利用原始httpUrlConnection发送post数据,设计到httpUrlConnection类的相关知识,感兴趣的朋友跟着小编一起学习吧
HttpURLConnection的get,post和图片加载
这是一个关于httpurlconnection 的网络post get 图片下载的Demo
HttpURLConnectionTest_Get_Post 里面包含HttpURLConnection实现的Get和post方法
基于HttpURLConnection的网络访问 已封装好,一句话访问网络 可采用GET和POST两种方式访问
NULL 博文链接:https://zjutsoft.iteye.com/blog/1313754
基于HttpURLConnection的封装http请求,进行了https校验忽略,对get和post进行了整合,更方便请求,对header以及超时的进行了可配置化处理,对返回也进行了封装,便于请求结果的处理。