java 发送post和get请求源码详解
来源:互联网 发布:安卓看电影软件 编辑:程序博客网 时间:2024/06/05 07:47
package com.hzau.util;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;/** * 调用网络接口得到相应的消息 * @author hufan * */public class GetInterfaceMsg {public static void main(String[] args) {String test=sendPost("http://localhost:8080/Retrospect/eis","pt=12345678902700671110");System.out.println(test);}/** * get方法 * @param url * @return */ public static String getJsonline (String url) { StringBuilder jsonLine = new StringBuilder(); try {URL myurl = new URL(url);URLConnection urlconn = myurl.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(urlconn.getInputStream(),"UTF-8"));String inputLine = null;while ( (inputLine = in.readLine()) != null) {jsonLine.append(inputLine);}in.close();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return jsonLine ; }/** * 发送post请求 * @param url * @param param * @return */public static String sendPost(String url,String param){PrintWriter out=null;BufferedReader in=null;String result="";try {URL myUrl=new URL(url); //打开和url之间的连接URLConnection conn=myUrl.openConnection();// 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//发送post请求必须设置如下两行 conn.setDoInput(true); conn.setDoOutput(true); //获取输出流 out=new PrintWriter(conn.getOutputStream()); //发送请求参数 out.print(param); //清空输出流缓冲 out.flush(); //读取url的响应in=new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while((line=in.readLine())!=null){result+=line;}} catch (Exception e) {e.printStackTrace();}finally{//关闭输入输出流try {if(out!=null){out.close();}if(in!=null){in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result;}}
0 0
- java 发送post和get请求源码详解
- java 发送get和post请求
- java 发送get和post请求
- java发送http get 和 post 请求
- java发送post请求和get
- Java发送GET和POST请求
- JAVA发送post和get请求
- java 发送get和post请求
- Java后台发送Post和get请求
- Java 发送GET和POST请求
- java发送POST和GET请求
- Java实现发送Get和Post请求
- Java发送Get/Post请求
- Java发送GET,POST请求
- JAVA发送POST+GET请求
- java发送get/post请求
- Java 发送 get, post 请求
- JAVA 发送 POST、GET请求
- 怎样在VS2013中安装配置boost_1_55_0库
- css如何li中选中后加上class属性js控制
- 欢迎使用CSDN-markdown编辑器
- 如何将网络流转化为内存流 C#
- 编程之美3.1—字符串移位包含问题
- java 发送post和get请求源码详解
- Android 正则表达式g
- C#中的Invoke
- 关于xml总结(二)
- 如何在Ubuntu QML应用中实现一个垂直的Slider
- GCDAsyncSocket接受数据丢包的问题
- 我用DW也有几年了,真不知道你说的这些代码是什么?不是快捷键吧?
- sax、dom、pull解析xml
- Android图片的选取和拍照