发送get请求
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpGetUtil {
public static void main(String[] args) {
HttpURLConnection conn = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL("http://www.xxx.com/xxx/check?orgCode=321300&waybillNo=773011197999686");
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//设置请求头(如果没有请求头信息,则不需要写这一项)
conn.setRequestProperty("appId", "app_service");
conn.connect();
inputStream = conn.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//拼接返回参数
StringBuilder sb = new StringBuilder();
String responseMsg = null;
while ((responseMsg = bufferedReader.readLine()) != null){
sb.append(responseMsg);
}
System.out.println(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(conn != null){
conn.disconnect();
}
}
}
}
- connect() 方法
1.这段代码已经使用url.openConnection()创建了连接,connect()方法可有可无。
2.如果使用HttpURLConnection conn=new HttpURLConnection(new URL(“路径”)),则需要使用connect()方法进行连接。
发送post请求
import com.alibaba.fastjson.JSON;
import com.mr.zhang.thread.dto.ParamDto;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpPostUtil {
public static void main(String[] args) {
HttpURLConnection conn = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL("http://www.xxx.com/xxx/xxxxxx/xxxxxxxxxxxxx");
conn = (HttpURLConnection) url.openConnection();//通过openConnection创建对象
conn.setRequestMethod("POST");
//设置请求体格式,这里设置的是json格式
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8;");
//连接服务器超时时间ms
conn.setConnectTimeout(5000);
//读取服务器返回超时时间ms
conn.setReadTimeout(5000);
//设置请求头
conn.setRequestProperty("appId", "app_service");
//设置请求体
ParamDto paramDto = new ParamDto();
paramDto.setOrgCode("222001");
paramDto.setDataType("INTERCEPT");
paramDto.setPdaCode("JSYG22200110001");
paramDto.setVersionNo("1573360821238");
String body = JSON.toJSONString(paramDto);
//设置成true,向远程服务器写数据
conn.setDoOutput(true);//默认是false,无法写入body
OutputStream outputStream = conn.getOutputStream();
outputStream.write(body.getBytes());
outputStream.flush();
outputStream.close();
inputStream = conn.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String responseMsg = null;
while ((responseMsg = bufferedReader.readLine()) != null){
sb.append(responseMsg);
}
System.out.println(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(conn != null){
conn.disconnect();
}
}
}
}
-
conn.setDoOutput(true);
默认值是false,向远程服务器写数据时,需要设置成true -
conn.setDoInput(true);
默认值是true,从远程服务器读数据时使用。因为默认是true,该参数可以不设置。