情景:
? ? ? ? 在發(fā)送短信的時(shí)候,要使用x-www-form-urlencoded的編碼格式進(jìn)行傳遞參數(shù)。?
具體要求:
參數(shù)名稱 |
說明 |
備注 |
userId |
用戶名 |
|
timespan |
時(shí)間戳 |
格式為yyyyMMddHHmmss |
password |
密碼 |
此處用原始密碼+時(shí)間戳 做MD5加密,32位大寫格式 ? |
phone |
手機(jī)號(hào) |
多個(gè)用英文逗號(hào)隔開 |
msgType |
編碼類型 |
選填,如果不填默認(rèn)為GBK,可以選填GBK 或者UTF8/UTF-8 |
content |
內(nèi)容 |
做base64加密操作,編碼方式 使用msgType中的方式,如果msgType有值的話 |
application/x-www-form-urlencoded 的情況下,restful那邊要怎么處理參數(shù)呢?
一,HTTP上傳的基本知識(shí) ? ? ?
? ? ? ? ?在Form元素的語(yǔ)法中,EncType表明提交數(shù)據(jù)的格式 用 Enctype 屬性指定將數(shù)據(jù)回發(fā)到服務(wù)器時(shí)瀏覽器使用的編碼類型。
?? ??? ? application/x-www-form-urlencoded: 窗體數(shù)據(jù)被編碼為名稱/值對(duì)。這是標(biāo)準(zhǔn)的編碼格式。?
?? ??? ? multipart/form-data: 窗體數(shù)據(jù)被編碼為一條消息,頁(yè)上的每個(gè)控件對(duì)應(yīng)消息中的一個(gè)部分。?
?? ??? ? text/plain:窗體數(shù)據(jù)以純文本形式進(jìn)行編碼,其中不含任何控件或格式字符。
補(bǔ)充
? ? ? ? ?form的enctype屬性為編碼方式,常用有兩種:application/x-www-form-urlencoded和multipart/form-data,默認(rèn)為application /x-www-form-urlencoded。
? ? ? ? ?當(dāng)action為get時(shí)候,瀏覽器用x-www-form-urlencoded的編碼方式把form數(shù)據(jù)轉(zhuǎn)換成一個(gè)字串(name1=value1& amp;name2=value2...),然后把這個(gè)字串a(chǎn)ppend到url后面,用?分割,加載這個(gè)新的url。
? ? ? ? ?當(dāng)action為post時(shí)候,瀏覽器把form數(shù)據(jù)封裝到http body中,然后發(fā)送到server。
? ? ? ? 如果沒有type=file的控件,用默認(rèn)的application/x-www-form-urlencoded就可以了。但是如果有type=file的話,就要用到multipart/form-data了。瀏覽器會(huì)把整個(gè)表單以控件為單位分割,并為每個(gè)部分加上 Content-Disposition(form-data或者file),Content-Type(默認(rèn)為text/plain),name(控件 name)等信息,并加上分割符(boundary)。
??這部分來自:
https://www.cnblogs.com/lexus/archive/2012/03/19/2405526.html#!comments
作者:lexus
2,處理
設(shè)置?application/x-www-form-urlencoded
設(shè)置表頭,參數(shù)用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("短信接口接口請(qǐng)求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("發(fā)送短信請(qǐng)求失敗", resultStr +" "+ResultCodeEnum.getCodeMsgMap().get(resultStr));
}
return resultStr;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("發(fā)送短信請(qǐng)求失敗", 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", "參數(shù)格式有誤"),ERROR_EXTEND_CODE("-105", "擴(kuò)展碼錯(cuò)誤"),
OVER_SIZE("-106", "內(nèi)容超長(zhǎng)(500字)或內(nèi)容為空"),ERROR_USER_STATE("-107", "用戶狀態(tài)異常"),
ERROR_AUTH_IP("-108", "Ip鑒權(quán)失敗"),NOT_PARSE_CONTENT("-109", "內(nèi)容解析異常"),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;
}
}
參數(shù)處理
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直接請(qǐng)求
文章來源:http://www.zghlxwxcb.cn/news/detail-623904.html
總結(jié):
? ? ? 使用restful請(qǐng)求application/x-www-form-urlencoded格式的內(nèi)容,需要設(shè)置表頭,參數(shù)用MultiValueMap<String, String> 的格式來處理。文章來源地址http://www.zghlxwxcb.cn/news/detail-623904.html
到了這里,關(guān)于java restful application/x-www-form-urlencoded 傳遞參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!