HttpClient POST请求

风格不统一 提交于 2020-11-24 18:45:51
@Test
public void testPost(){
//conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 接收参数不应该加@ResponseBody
//conn.setRequestProperty("Content-Type", "application/json"); 接收参数必须加@ResponseBody
String url = "http://192.168.40.156:8081/member/change/password/test";
String body = "oldPassword=111111qqq&passwordEnsure=111111qq&password=111111qq";
String result = HttpUtils.post(url,body);
System.out.println("result:" + System.lineSeparator() + result);
Result result1 = JSONObject.parseObject(result,Result.class);
System.out.println("code:"+result1.getStatus().getCode());
System.out.println("statusMsg:"+result1.getStatus().getStatusMsg());
System.out.println("data:"+result1.getData());
}
控制面板输出:

url:
http://192.168.40.156:8081/member/change/password/test
body:
oldPassword=111111qqq&passwordEnsure=111111qq&password=111111qq
result:
{"status":{"code":500,"statusMsg":"旧登录密码必须与原设定密码一致。"},"data":null}
code:500
statusMsg:旧登录密码必须与原设定密码一致。
data:null

请求地址:
@PostMapping("change/password/test")

//@RequestBody MemberChangePasswordDTO memberChangePasswordDTO 不行

public Result changePasswordTest(MemberChangePasswordDTO memberChangePasswordDTO) {

String mobilePhone = "18211672518";
String oldPassword = memberChangePasswordDTO.getOldPassword();
String password = memberChangePasswordDTO.getPassword();
String passwordEnsure = memberChangePasswordDTO.getPasswordEnsure();
//密码校验
//旧密码验证
boolean isValidOldPassword = memberService.verifyPassword(oldPassword,mobilePhone);
if(!isValidOldPassword){
return Result.error("旧登录密码必须与原设定密码一致。");
}
//新密码验证
if (!Pattern.matches(Constant.REGEX_PASSWORD, password)) {
return Result.error("密码的长度为8-16位,由大小写字母、数字、符号至少两种组成。");
}
if (!password.equals(passwordEnsure)) {
return Result.error("'确认新密码'与'新的登录密码'所输入的密码必须一致。");
}
if(Objects.equals(oldPassword,password)){
return Result.error("新旧密码相同,无需修改。");
}
int count = memberService.updatePasswordByPhone(mobilePhone, password);

if (count > 0) {
return Result.ok("密码变更成功");
}
return Result.error("密码变更失败");

}


/**
* post请求
*
* @param url 功能和操作
* @param body 要post的数据
*/
public static String post(String url, String body) {
System.out.println("url:" + System.lineSeparator() + url);
System.out.println("body:" + System.lineSeparator() + body);

String result = "";
try {
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();

// 设置连接参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 提交数据
OutputStream out = conn.getOutputStream();
out.write(body.getBytes());
out.flush();
out.close();
// 读取返回数据
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true; // 读第一行不加换行符
while ((line = in.readLine()) != null) {
if (firstLine) {
firstLine = false;
} else {
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

附录1:返回数据的数据结构
public class Result<T> {
private Status status;

private T data;

public Result() {
}
public static Result ok() {
return new Result(Status.ok());
}

public static <T> Result<T> ok(T data) {
return new Result(Status.ok(), data);
}
public static <T> Result<T> error(String errorMsg) {
return new Result(Status.error(errorMsg));
}
public class Status implements StatusConstant {
private int code;
private String statusMsg;
public static Status ok() {
return new Status(SUCCESS, STATUS_OK);
}
public static Status error() {
return new Status(INTERNAL_ERR, STATUS_INTERNAL_ERR);
}

public static Status error(String msg) {
return new Status(INTERNAL_ERR, msg);
}
}
public interface StatusConstant extends ResultCode {
String STATUS_OK = "OK";
String STATUS_NOT_FOUND = "Not Found";
String STATUS_PARAMS_INVALID = "Params Invalid";
String STATUS_INTERNAL_ERR = "Server Internal Error";
}
public interface ResultCode {
int SUCCESS = 200;
int BAD_REQUEST = 400;
int UNAUTHORIZED = 401;
int FORBIDDEN = 403;
int NOT_FOUND = 404;
int METHOD_ALLOWED = 405;
int PARAMS_INVALID = 412;
int INTERNAL_ERR = 500;
}
附录2:入参的数据结构
/**
* @ClassName: MemberChangePasswordDTO
* @Description: 密码变更
* @author: yaozhenhua
* @date: 2019/3/26 15:26
*/
@Data
public class MemberChangePasswordDTO {
private String oldPassword;
private String password;
private String passwordEnsure;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!