失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > JAVA post和get方式请求远程HTTP接口

JAVA post和get方式请求远程HTTP接口

时间:2023-01-24 11:58:42

相关推荐

JAVA post和get方式请求远程HTTP接口

java发送http协议 一般对方都会限定post或者get 这里就是可用的方法

首先是POST方式

传入参数为json 可改为任何类型

public class HttpPost implements Runnable {private String url_str;public HttpPost () {}public HttpPost (String url, boolean isPost) {this.url_str = url;}@Overridepublic void run() {JSONObject param_json = new JSONObject();URL url = null;PrintWriter send_out = null;BufferedReader send_in = null;String result = "";try {//----------------------------------------------------------/////thl331860203/article/details/51783434////JAVA POST请求远程HTTP接口////----------------------------------------------------------// url = new URL(this.url_str);//打开和URL之间的连接 URLConnection connection = url.openConnection();//设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");end_out = new PrintWriter(connection.getOutputStream());

//发送请求参数 send_out.print(param_json);//flush输出流的缓冲 send_out.flush();//定义BufferedReader输入流来读取URL的响应send_in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line;while((line = send_in.readLine()) != null){result += line;}getResultData(result);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void getResultData(String result) {}}

接下来是Get方式

public class HttpGet implements Runnable {private String url_str;public HttpGet() {}public HttpGet(String url, boolean isPost) {this.url_str = url;} @Overridepublic void run() {JSONObject param_json = new JSONObject();URL url = null;PrintWriter send_out = null;BufferedReader send_in = null;String result = "";try {//----------------------------------------------------------/////thl331860203/article/details/51783434////JAVA POST请求远程HTTP接口////----------------------------------------------------------// url = new URL(this.url_str);//打开和URL之间的连接 URLConnection connection = url.openConnection();//设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//建立连接 connection.connect();//定义BufferedReader输入流来读取URL的响应send_in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line;while((line = send_in.readLine()) != null){result += line;}getResultData(result);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void getResultData(String result) {}}

关于实现

HttpGet test = new HttpGet("");Thread thread = new Thread(test);thread.start();

因为JAVA在某版本之后 为了防止协议没有响应 所以不允许直接使用这个方法 一定要在子线程中使用

以上除了代码以外都是自己的理解 如果不对 请指出 谢谢

如果觉得《JAVA post和get方式请求远程HTTP接口》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。