import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import android.content.Context; /** * 向服务器端发送POST数据 * @author LinHang_He * */ public class PostUtils { private String path; private Context context=null; public PostUtils(String path) { this.path=path; } /** * 构造函数 * @param path 请求URL地址 * @param context 当前上下文对象 */ public PostUtils(String path,Context context) { this.path=path; this.context=context; } /** * 发送POST请求 * @param str 向服务器端发送的数据 * @return */ public String setPostRequest(String str){ try { byte [] entity=str.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 outStream=conn.getOutputStream(); outStream.write(entity); InputStream is=conn.getInputStream(); if(conn.getResponseCode()==200) { return inputStreamToString(is,"utf-8"); } else{ System.out.println("error"); return "timeout"; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return "timeout"; } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "timeout"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "timeout"; } } /** * 将字符流转换成字符串 * @param is 输入数据流 * @param encoding 字符编码 * @return */ private String inputStreamToString(InputStream is, String encoding) { try { // byte[] b = new byte[1024]; byte[] b = new byte[1]; String res = ""; if (is == null) { return ""; } int bytesRead = 0; while (true) { bytesRead = is.read(b, 0, 1); // return final read bytes counts if (bytesRead == -1) {// end of InputStream return res; } res += new String(b, 0, bytesRead, encoding); // convert to string using bytes } } catch (Exception e) { e.printStackTrace(); System.out.print("Exception: " + e); return ""; } } }
相关推荐
HTTP发送POST请求的工具类
http通用工具类
post,get方式请求接口封装,参数加密方式请求,参数RSA签名, 转换字符集到utf8,base64编码字符串
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
http请求工具类 常见的post get请求
封装了https发送Get和Post请求工的具类。直接看Demo例子。样例实现参考 HTTP服务POST方式代码
http请求提交类工具类,可以用main方法直接测试
Java发送http请求、https请求,post和get都支持
android http 工具类 get post
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
这是一个http请求工具类,里面有get接口请求和post接口请求传递map或json类型的参数的方法,里面还有put和delete的请求方法,此工具类可以在项目中直接使用
项目参考我写的博客:https://blog.csdn.net/qq_37835111/article/details/87255134
下面小编就为大家带来一篇HttpUtils 发送http请求工具类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
java的get和post请求,获取json的工具类,https时会存在ssl校验的问题,工具会自动去除ssl校验。
http的post请求跟get请求工具类,传入参数就能进行调用,以及返回接口值
这是一个java发送get、post请求,并得到返回结果的工具类。
public static String REQUEST_METHOD_POST = "POST"; public static String REQUEST_METHOD_GET = "GET"; // 媒体类型 public static String MIME_TYPE_FORM = "application/x-www-form-urlencoded;charset=...
Http工具类
Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...