客户端代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import org.apache.commons.httpclient.HttpStatus;
import java.net.URL;
import org.springframework.util.Assert;
public class TestClient {
public static void main(String[] args) throws Exception {
TestClient aaClient = new TestClient();
String resString =aaClient.doPostRequest("http://localhost:8889/SSM_HTTPS", "client to server", Charset.forName("UTF-8"), 10, 10);
System.out.println("返回数据:"+resString);
}
/**
* @param reqUrl 地址
* @param params 数据(json)
* @param charset 编码
* @param readTimeSec
* @param connTimeSec
* @return Http请求返回数据字符串
* @throws Exception
*/
public static String doPostRequest(String reqUrl, String params,
Charset charset, int readTimeSec, int connTimeSec)
throws Exception {
try {
Assert.hasText(reqUrl, "请求地址为空");
Assert.hasText(params, "请求数据为空");
} catch (Exception e) {
throw new Exception("发送请求参数为空:"
+ e.getMessage(), e);
}
String request = params ;//发送服务端数据
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) new URL(reqUrl)
.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setUseCaches(false);
httpConnection.setRequestProperty("Connection", "Keep-Alive");
httpConnection.setRequestProperty("Charset", charset.displayName());
httpConnection.setRequestProperty("Content-Type",
"application/json; charset=" + charset.displayName());
httpConnection.setRequestProperty("accept", "application/json");
httpConnection.setReadTimeout(readTimeSec * 1000);// 设置http连接的读超时,单位是毫秒
httpConnection.setConnectTimeout(connTimeSec * 1000);
// 1、连接
try {
httpConnection.connect();
} catch (Exception e) {
throw new Exception(
"服务器连接是失败");
}
OutputStream outwritestream = null;
// 2、发送数据
try {
System.out.println("请求数据:"+request);
byte[] writebytes = request.getBytes(charset);
outwritestream = httpConnection.getOutputStream();
outwritestream.write(writebytes);
outwritestream.flush();
} catch (Exception e) {
throw new Exception(
"请求数据发送失败", e);
} finally {
try {
outwritestream.close();
} catch (Exception e) {
}
}
return getHttpReturn(httpConnection , charset);
} catch (Exception e) {
throw new Exception("服务器连接失败:"
+ e.getMessage(), e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
/**
* @param httpConnection
* @return Http连接返回数据转化成字符串
* @throws Exception
*/
private static String getHttpReturn(HttpURLConnection httpConnection , Charset charset)
throws Exception {
int responseCode = -1;
try {
responseCode = httpConnection.getResponseCode();
System.out.println("responseCode:"+responseCode);
} catch (Exception e) {
throw new Exception(
"接收数据超时", e);
}
try {
Assert.isTrue(responseCode == HttpStatus.SC_OK, "服务器响应码【"
+ responseCode + "】");
} catch (Exception e) {
throw new Exception(
"接收数据失败:" + e.getMessage(), e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
httpConnection.getInputStream(), charset));
String str = "";
String temp;
while((temp = reader.readLine())!=null){
str = str + temp +"\r\n" ;
}
return str;
} catch (Exception e) {
throw new Exception(
"接收数据超时:" + e.getMessage(), e);
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (Exception e) {
}
}
}
}
服务端代码:
参考我的另一篇服务端代码博客 https://blog.csdn.net/DGH2430284817/article/details/86629578
测试:
先启动服务端,再运行客户端代码。
测试结果:
服务端:
服务器开启
接收到一个请求,请求地址:/127.0.0.1:59269
报文头数据:POST /SSM_HTTPS HTTP/1.1
该请求为POST请求
报文头数据:Charset: UTF-8
报文头数据:Content-Type: application/json; charset=UTF-8
报文头数据:accept: application/json
报文头数据:Cache-Control: no-cache
报文头数据:Pragma: no-cache
报文头数据:User-Agent: Java/1.6.0_45
报文头数据:Host: localhost:8889
报文头数据:Connection: keep-alive
报文头数据:Content-Length: 16
POST参数长度是:16
http服务端接收客户端数据:client to server
发送回执
http服务器返回客户端数据:server return value
客户端:
请求数据:client to server
responseCode:200
返回数据:server return value