http 原生post 请求
来源:互联网 发布:sms水动力学软件 编辑:程序博客网 时间:2024/06/05 03:05
进行http 原生post 请求
请求参数的_$content 为 name=value1&name2=value2
public static String HttpPost(String _$url, String _$content) { return HttpPost(_$url, _$content, 5000, 5000, "UTF-8"); } public static String HttpPost(String _$url, String _$content, String _$charsetName) { return HttpPost(_$url, _$content, 5000, 5000, _$charsetName); } public static String HttpPost(String _$url, String _$content, int _$connectTimeout, int _$readTimeout, String _$charsetName) { String responseString = null; URL url = null; HttpURLConnection http = null; try { url = new URL(_$url); http = (HttpURLConnection)url.openConnection(); http.setDoOutput(true); http.setDoInput(true); http.setUseCaches(false); http.setRequestMethod("POST"); http.setConnectTimeout(_$connectTimeout); http.setReadTimeout(_$readTimeout); byte[] requestStringBytes = _$content.getBytes(_$charsetName); http.setRequestProperty("Content-length", ""+requestStringBytes.length); http.setRequestProperty("Content-Type", "application/octet-stream"); http.setRequestProperty("Connection", "Keep-Alive"); http.setRequestProperty("Charset", _$charsetName); OutputStream outputStream = http.getOutputStream(); outputStream.write(requestStringBytes); outputStream.close(); int responseCode = http.getResponseCode(); if (200 == responseCode) { StringBuffer sb = new StringBuffer(); BufferedReader responseReader = new BufferedReader(new InputStreamReader( http.getInputStream(), _$charsetName)); String readLine; while ((readLine = responseReader.readLine()) != null) { sb.append(readLine); } responseReader.close(); responseString = sb.toString(); } } catch (Exception e) { System.out.println("HttpPost error:" + e); return ""; } finally { if (http != null) { http.disconnect(); } } return responseString; }
0 0
- http 原生post 请求
- java原生http请求post
- iOS原生Http请求,get post 异步
- java原生http请求post&get
- 【Go】原生http get和post请求框架
- 原生ajax POST请求
- java原生http请求
- 原生的http请求
- android--原生http请求
- 原生ajax发送post请求
- android http post请求
- http的post请求
- C# http post请求
- HTTP 模拟post请求
- http post get 请求
- http请求post
- ios HTTP post请求
- HTTP GET/POST 请求
- WebService注解汇总
- 程序员面试金典1.7:若M*N矩阵中某个元素为0,则将其所在的行与列清零
- 类加载器
- C#中的多态
- luasql-master的修改,支持调用存储过程
- http 原生post 请求
- 类和对象
- 在 Activity 之间传递参数————传递值对象
- 状态机的原理简析及重要用途
- 百度地图获取当前位置坐标,并计算距离
- C#中sealed关键字
- 数据结构基础笔记(一)【严蔚敏】
- noip2003 栈 (卡特兰数)
- shell programing