问题描述
- http 415错误怎么解决?
-
public String sendPost(String jsonStr) throws IOException {
String path="http://183.57.42.117:8081/ca/yunxin/risk";
//String path="http://www.baidu.com";
byte[] data = jsonStr.getBytes();URL url = new URL(path);
PrintWriter out = null;
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(jsonStr);
// flush输出流的缓冲
out.flush();
System.out.println("conn.getResponseCode()=:"+conn.getResponseCode());
StringBuffer msg=new StringBuffer();
// System.out.println("conn.getResponseCode()=:"+conn.getResponseCode());
if (conn.getResponseCode() == 200) {
System.out.println("conn.getResponseCode()=:"+conn.getResponseCode());
BufferedReader in = new BufferedReader(new InputStreamReader(
(InputStream) conn.getInputStream(), "UTF-8"));while(in.readLine()!=null){ msg.append(in.readLine()); } System.out.println("msg=:"+msg); in.close(); } conn.disconnect(); return msg.toString(); } 就是我得到的响应码是415
界面通过form表单提交到Controller Controller得到表单数据存在Map转为json
然后调用远程接口 得到的响应码就是415 这个怎么解决啊?
解决方案
HTTP 415 错误 – 不支持的媒体类型。类型不一致导致你的web出现此类状况