@RequestMapping(params = "method=testsms")
public void testsms() {
String smsUrl = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_url");
String smsUsername = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_username");
String smsPassword = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_password");
String httpbasicInfo = null;
try {
httpbasicInfo = "Basic "
+ Base64.encode((smsUsername + ":" + smsPassword)
.getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
logger.info(httpbasicInfo);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization", httpbasicInfo);
httpHeaders.add("Content-Type", "application/json");
LinkedMultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<String, Object>();
bodyMap.add("receiver", "18600105009");
bodyMap.add("content", "tgest");
bodyMap.add("channelType", "1");
bodyMap.add("msgType", "1");
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity("{\"receiver\":\"18600105009\",\"content\":\"test\",\"channelType\":1,\"msgType\":1}", httpHeaders);
ResponseEntity<String> resStr = restTemplate.exchange(
"http://nbmsrest.ebnew.com/rest/message/v1/send/",
HttpMethod.POST, httpEntity, String.class);
System.out.println(resStr.getBody());
}
@RequestMapping(params = "method=testsms2")
public void testsms2() {
String smsUrl = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_url");
String smsUsername = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_username");
String smsPassword = (String) ProDefineConfigurer
.getContextProperty("bilian_sms_password");
String httpbasicInfo = null;
try {
httpbasicInfo = "Basic "
+ Base64.encode((smsUsername + ":" + smsPassword)
.getBytes("utf-8"));
logger.info(httpbasicInfo);
URL url1 = new URL(
"http://nbmsrest.ebnew.com/rest/message/v1/send/");
String json = "{\"receiver\":\"18600105009\",\"content\":\"test\",\"channelType\":1,\"msgType\":1}";
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
String author = httpbasicInfo;
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", author);
conn.setRequestProperty("Content-Length",
String.valueOf(json.getBytes("UTF-8").length));
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.connect();
OutputStream out = conn.getOutputStream();
out.write(json.getBytes("UTF-8"));
out.flush();
out.close();
System.out.println(conn.getResponseCode());
if (conn.getResponseCode() == 200) {
System.out.println("连接成功");
// 请求返回的数据
InputStream in = conn.getInputStream();
String a = null;
byte[] data2 = new byte[in.available()];
in.read(data2);
a = new String(data2);
System.out.println("返回:" + a);
} else {
System.out.println("no++");
}
System.out.println(conn.getResponseCode());
} catch (Exception e) {
e.printStackTrace();
}
}
<bean id="simpleRestTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="formHttpMessageConverter"
class="org.springframework.http.converter.FormHttpMessageConverter" />
</list>
</property>
</bean>

很多时候,我们用 Ajax 提交数据时,也是使用这种方式。例如 JQuery 和 QWrap 的 Ajax,Content-Type 默认值都是「application/x-www-form-urlencoded;charset=utf-8」。