简单的一个HttpUrlConnect的post请求
来源:互联网 发布:centos 下载 编辑:程序博客网 时间:2024/06/05 12:04
public static String postUrlConnect(String urlPath, Map<String,Object> map){ StringBuffer sbRequest =new StringBuffer(); if(map!=null&&map.size()>0){ for (String key:map.keySet()){ sbRequest.append(key+"="+map.get(key)+"&"); } } String request = sbRequest.substring(0,sbRequest.length()-1); try { //创建URL URL url = new URL(urlPath); //由URL的openConnection方法得到一个HttpURLConnection(需要强转) HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); //设置post提交 httpURLConnection.setRequestMethod("POST"); //设置超时时间 httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(30000); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); //把请求正文通过OutputStream发出去 OutputStream os =httpURLConnection.getOutputStream(); os.write(request.getBytes()); os.flush(); //判断响应码 200 代表成功 if(httpURLConnection.getResponseCode()==200){ //由HttpURLConnection拿到输入流 InputStream in=httpURLConnection.getInputStream(); StringBuffer sb=new StringBuffer(); //根据输入流做一些IO操作 byte [] buff =new byte[1024]; int len=-1; while((len=in.read(buff))!=-1){ sb.append(new String(buff,0,len,"utf-8")); } in.close(); os.close(); httpURLConnection.disconnect(); return sb.toString(); }else{ return null; } }catch (Exception e){ Log.e("post","code:"+e.getMessage()); return null; } }}
1 0
- 简单的一个HttpUrlConnect的post请求
- HttpUrlConnect Get与Post请求
- 异步任务HttpUrlconnect 的GET 网络请求
- HttpUrlConnect GET()和POST()方法请求
- Android实例demo15之HttpUrlConnect的get、post操作、asynchttp
- 一个ajax的Post请求
- 简单的POST请求和get请求
- 一个angular的post请求的坑
- Python发送POST请求的简单例子
- 最简单的Java Post请求
- php 简单的web post请求处理
- Swift3 1.简单的网络请求(POST)
- Python爬虫的post请求简单实例
- 简单的 http 发送异步post请求
- HttpWebRequest post请求的一个例子
- AFNetworking 3.0 POST请求的一个坑
- 参数传到请求中返回信息。 简单的post请求
- okgo简单的GET请求 和POST请求
- Oracle12c中数据泵新特性之功能增强(expdp, impdp)
- 针对页面加载的问题
- 【数据结构】B树的详解
- leetcode-35. Search Insert Position
- C++中的STL以及库函数在ACM中的运用
- 简单的一个HttpUrlConnect的post请求
- weex初始的坎坷之路
- Mybatis使用注意事项_2
- 五月联考的总(lao)结(sao)
- js自定义事件
- linux如何查看系统信息
- 码农技术炒股之路——实时交易信息、主力动向信息分库备份
- 【数据结构】对称矩阵和稀疏矩阵的压缩存储
- Directx