java restful application/x-www-form-urlencoded 传递参数

情景:

        在发送短信的时候,要使用x-www-form-urlencoded的编码格式进行传递参数。 

具体要求:

参数名称

说明

备注

userId

用户名

timespan

时间戳

格式为yyyyMMddHHmmss

password

密码

此处用原始密码+时间戳

做MD5加密,32位大写格式

 

phone

手机号

多个用英文逗号隔开

msgType

编码类型

选填,如果不填默认为GBK,可以选填GBK

或者UTF8/UTF-8

content

内容

做base64加密操作,编码方式

使用msgType中的方式,如果msgType有值的话

application/x-www-form-urlencoded 的情况下,restful那边要怎么处理参数呢?

一,HTTP上传的基本知识      

         在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。
         application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 
         multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。 
         text/plain:窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
补充
         form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application /x-www-form-urlencoded。

         当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1& amp;name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。

         当action为post时候,浏览器把form数据封装到http body中,然后发送到server。

        如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上 Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件 name)等信息,并加上分割符(boundary)。

  这部分来自:

https://www.cnblogs.com/lexus/archive/2012/03/19/2405526.html#!comments

作者:lexus

2,处理

设置 application/x-www-form-urlencoded

设置表头,参数用MultiValueMap<String, String> 的格式

  public String sendNoticeContent(MultiValueMap<String, String> param) {
        String url = "xxxx";
         
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            RestTemplate restTemplate=new RestTemplate();
            HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<MultiValueMap<String, String>>(param, headers);
            logger.info("短信接口接口请求url:{}, param: {}", url, JSON.toJSONString(httpEntity));
            String resultStr = restTemplate.postForObject(url, param, String.class);
            logger.info("sendNoticeContent result {}", resultStr);
            // 如果返回字符串以-开头,也则提交失败,否则为提交成功
            if(StringUtils.isBlank(resultStr) || resultStr.startsWith("-")){
                throw new Exception("发送短信请求失败", resultStr +" "+ResultCodeEnum.getCodeMsgMap().get(resultStr));
            }
            return resultStr;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("发送短信请求失败", e.getMessage());
        }
    }

异常码枚举类

import java.util.HashMap;
import java.util.Map;

public enum ResultCodeEnum {

    NOT_USER("-101", "用户不存在"),ERROR_PWD("-102", "密码不正确"),NOT_SUFFICIENT_FUNDS("-103", "余额不足"),
    ERROR_PARAM("-104", "参数格式有误"),ERROR_EXTEND_CODE("-105", "扩展码错误"),
    OVER_SIZE("-106", "内容超长(500字)或内容为空"),ERROR_USER_STATE("-107", "用户状态异常"),
    ERROR_AUTH_IP("-108", "Ip鉴权失败"),NOT_PARSE_CONTENT("-109", "内容解析异常"),UNKNOW_ERROR("-990", "未知异常");

    private String code;
    private String msg;

    ResultCodeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static Map<String, String> getCodeMsgMap() {
        Map<String, String> codeMsgMap = new HashMap<>();
        for (ResultCodeEnum m : ResultCodeEnum.values()) {
            codeMsgMap.put(m.getCode(), m.getMsg());
        }
        return codeMsgMap;
    }
}

参数处理

public String sendNoticeConent(String userId, String password, String content, String phone){
        String timespan = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");

        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
        params.put("userId", Collections.singletonList(userId));
        params.put("password", Collections.singletonList(Md5Utils.md5WithTimeStamp(password, timespan)));
        params.put("timespan", Collections.singletonList(timespan));
        params.put("content", Collections.singletonList(Base64Utils.encode(content)));
        params.put("phone", Collections.singletonList(phone));
        params.put("msgType", Collections.singletonList("UTF-8")); // 跟encode保持一致
        return restResponsibleService.sendNoticeContent(params);
    }

MD5加密

引用包:

implementation 'commons-codec:commons-codec:1.9'

代码:


    public static String md5WithTimeStamp(String text, String currentTimeSuffix ){
        String content = text+currentTimeSuffix;
        return DigestUtils.md5Hex(content).toUpperCase();
    }

base64

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.regex.Pattern;

public class Base64Utils {
    public static void main(String args[]) {
        String sql = "select * from staff";
        System.out.println(sql);
        System.out.println(encode(sql));
        System.out.println(decode(encode(sql)));
        System.out.println(isBase64(encode(sql)));
        System.out.println(isBase64(decode(encode(sql))));
    }

    private static final String UTF_8 = "UTF-8";
    private static Base64.Encoder encoder;
    //即为安全的编码方式,替换“+” “/” “-”为“_”
    private static Base64.Decoder decoder;

    static {
        encoder = Base64.getEncoder();
        decoder = Base64.getDecoder();
    }
    //encode
    public static byte[] encode(byte[] bytes) {
        return encoder.encode(bytes);
    }

    public static String encode(String content) {
        byte[] encode = encode(content.getBytes());
        try {
            return new String(encode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String encode2String(byte[] bytes) {
        return encoder.encodeToString(bytes);
    }

    public static byte[] encode2Byte(String content) {
        return encode(content.getBytes());
    }

    public static byte[] decode(byte[] bytes) {
        return decoder.decode(bytes);
    }

    public static byte[] decode2Byte(String content) {
        return decoder.decode(content.getBytes());
    }

    public static String decode2String(byte[] bytes) {
        try {
            return new String(decoder.decode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decode(String content) {
        byte[] decode = decode(content.getBytes());
        try {
            return new String(decode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Boolean isBase64(String str) {
         String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
         return Pattern.matches(base64Pattern, str);
    }
}

postman直接请求

总结:

      使用restful请求application/x-www-form-urlencoded格式的内容,需要设置表头,参数用MultiValueMap<String, String> 的格式来处理。

猜你喜欢

转载自blog.csdn.net/qq_35461948/article/details/130867629
今日推荐