资源简介
java调用HTTP接口(Get请求和Post请求)
代码片段和文件信息
package com.company;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 功能简述:
*
* @author caidingnu
* @create 2019/03/27 13:46
* @since 1.0.0
*/
public class HTTP_Request {
private static void httpURLGETCase() {
String methodUrl = “http://127.0.0.1:8099/select_by_id?userid=1“;
HttpURLConnection connection = null;
BufferedReader reader = null;
String line = null;
try {
URL url = new URL(methodUrl);
connection = (HttpURLConnection) url.openConnection();// 根据URL生成HttpURLConnection
connection.setRequestMethod(“GET“);// 默认GET请求
connection.connect();// 建立TCP连接
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream() “UTF-8“));// 发送http请求
StringBuilder result = new StringBuilder();
// 循环读取流
while ((line = reader.readLine()) != null) {
result.append(line).append(System.getProperty(“line.separator“));// “\n“
}
System.out.println(result.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
connection.disconnect();
}
}
//Post请求
private static void httpURLPOSTCase() {
String methodUrl = “http://127.0.0.1:8099/select_by_id?userid=1“;
HttpURLConnection connection = null;
OutputStream dataout = null;
BufferedReader reader = null;
String line = null;
try {
URL url = new URL(methodUrl);
connection = (HttpURLConnection) url.openConnection();// 根据URL生成HttpURLConnection
connection
- 上一篇:闹钟java源码
- 下一篇:java 多个实习报告
相关资源
- java调用cplex必须的jar包:cplex.jar
- java调用海康sdk教程-linux/win.zip
- 支付宝支付接口java调用
- java调用打印机
- 最全的java调用matlab总结
- NetBetBeans 编写Java调用DLL代码
- LIBSVM工具包可以在weka中使用并被JAV
- java调用高德公交api(java源码)
- pos机java调用程序demo(基于dll调用)
- java调用C#封装的dll(jnacom)
- SparkSQL HDFS JAVA调用完整
- java调用C#封装的dll方法
- java调用打印机指令实现
- [测试通过]JAVA调用第三方DLL的简单方
- JAVA调用Webservice接口Demo
- JAVA调用C#的DLL文件
- Java调用C#DLL
- JAVA调用DLL:jna-5.1.0
- Java调用Zebra条码打印机打印条码、中
- JAVA调用斑马打印机打印
- Java调用腾讯云接口发送短信验证码项
- java调用AutoCAD绘图
- java调用 webservice接口
- java调用 webservice
- Java调用摄像头进行拍照
- JAVA调用百度接口实现人脸识别完整代
- java 访问 wcf 使用的HttpClient
- java调用webservice的demo
- java调用一个URL接口的方法类
- java调用AutoCAD接口绘图工具类
评论
共有 条评论