post发送请求
来源:互联网 发布:淘宝宝贝链接 编辑:程序博客网 时间:2024/04/27 16:04
public static String sendPost(String url, Map<String, String> params) { URL u = null; HttpURLConnection con = null; // 构建请求参数 StringBuffer sb = new StringBuffer(); if (params != null) { for (Entry<String, String> e : params.entrySet()) { sb.append(e.getKey()); sb.append("="); sb.append(e.getValue()); sb.append("&"); } sb.substring(0, sb.length() - 1); } // 尝试发送请求 try { u = new URL(url); con = (HttpURLConnection) u.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStreamWriter osw = new OutputStreamWriter( con.getOutputStream(), "UTF-8"); osw.write(sb.toString()); osw.flush(); osw.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.disconnect(); } } // 读取返回内容 StringBuffer buffer = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader( con.getInputStream(), "UTF-8")); String temp; while ((temp = br.readLine()) != null) { buffer.append(temp); buffer.append("\n"); } } catch (Exception e) { e.printStackTrace(); } return buffer.toString(); }
阅读全文
0 0
- IWebBrowser2 发送post请求
- WebClient发送POST请求
- android发送post请求
- Httpclient发送POST请求
- .NET发送POST请求
- IWebBrowser2 发送post请求
- httpClient发送post请求
- python发送post请求
- Android发送post请求
- Android发送post请求
- Ajax发送POST请求
- python发送post请求
- Android发送post请求
- C++ 发送POST请求
- cakephp发送post请求
- AFNetWorking发送post请求
- linux发送post请求
- php发送post请求
- ES6学习之路4----字符串模版
- Redis学习笔记(3)hash的读写
- JSONObject使用方法详解
- ubuntu caffe 编译
- 第27篇:Angular中的$q的形象解释及深入用法
- post发送请求
- 理解spring中AutoWire,Reource,以及Service等注解
- python3 多继承和MRO顺序
- OWL文件和protege的关系(加深理解版)
- 微信开放平台修改微信分享的小图标后,移动端显示的分享的小图标未改变的问题解决
- Elasticsearch、Logstash、Kibana、Filebeat的使用总结
- Broadcasts详解
- 线程和Service区别
- iOS出现NSString* class_name = ***之类的错误的解决方法