http post上传数据
来源:互联网 发布:大数据融合技术 kettle 编辑:程序博客网 时间:2024/06/04 19:45
import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import net.sf.json.JSONObject;public class HttpUtils { public static void appadd(String ADD_URL,JSONObject josnObj) { try { URL url = new URL(ADD_URL); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("contentType", "utf-8"); connection.connect(); //POST请求 DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write(josnObj.toString().getBytes("UTF-8")); System.out.println("data=="+josnObj.toString()); out.flush(); out.close(); //读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } System.out.println(sb.toString()); reader.close(); connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
测试代码
import net.sf.json.JSONObject; public class Test(){ public static void main(String[] args) { JSONObject josnObj = new JSONObject(); josnObj.put("type", "bb"); josnObj.put("name", "aa"); HttpUtils.appadd("http://127.0.0.1:8080/test", josnObj); } }
0 0
- http post上传数据
- HTTP post 上传
- http post 上传文件
- HTTP POST上传
- HTTP POST上传文件
- C语言 如何用http post方式 上传json数据
- Http multipart/form-data多参数Post方式上传数据
- POST上传JSON数据
- POST上传JSON数据
- iPhone http Post数据
- iPhone http Post数据
- http post提交数据
- http post提交数据
- php http post数据
- HTTP-POST数据
- http post 文件上传解析
- http协议 文件上传 POST
- HTTP post上传 小纪
- GUID与字符串之间的恩恩怨怨
- 计算机视觉库OpenCV初步了解
- Nagios系统监控指标扩展
- 绘制点、线、面
- 线段树内容详解
- http post上传数据
- Caffe傻瓜系列(10):draw_net.py绘制caffe net结构
- GeckoFx (6)替换、禁用系统弹窗警告
- redis扩展ibatis缓存
- Caffe傻瓜系列(11):caffe中的lr_policy选择
- perl的hello world
- Android资源文件中颜色使用的总结
- Spring 动态管理定时任务(使用quartz) 只是管理启动时间 不能做启动和暂停
- Garbage Collection | Mark-Sweep算法