使用POST发送JSON对象时使用HTTP 415 [英] HTTP 415 while sending JSON object using POST

查看:214
本文介绍了使用POST发送JSON对象时使用HTTP 415的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.io.BufferedReader;    
import java.io.InputStreamReader;    
import java.net.HttpURLConnection;    
import java.net.URL;    
import java.io.DataOutputStream;        
import java.io.InputStream;

public class TestingPost {

 public static void main(String args[]) {

    URL url;
    HttpURLConnection connection = null;  
    String targetURL=".....";//here is my local server url
    String urlParameters="{\"clubhash\":\"100457d41b9-ab22-4825-9393-ac7f6e8ff961\",\"username\":\"anonymous\",\"message\":\"simply awesome\",\"timestamp\":\"2012/11/05 13:00:00\"}";

    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");

      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  

      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      System.out.println("message="+response.toString());

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      if(connection != null) {
        connection.disconnect(); 
      }
    }
  }

}

我正在尝试使用HTTP POST方法发送JSON对象.上面是代码,但是我得到了

I am trying to send a JSON Object using the HTTP POST method. Above is the code but I am getting

java.io.IOException: Server returned HTTP response code: 415 for URL: ....
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at TestingPost.main(TestingPost.java:38)"

我的代码有什么问题?

推荐答案

您获得的HTTP响应代码为

The HTTP response code you get is

415 Unsupported Media Type

这意味着服务器无法处理您发送给它的格式.您的HTTP请求设置了此标头:

This means that the server can not handle the format you sent to it. Your HTTP request sets this header:

Content-Type: application/x-www-form-urlencoded

这是提交表单时浏览器发送的内容类型.如果要发送JSON,请使用以下标头:

This is the content type sent by a browser if a form is submitted. If you want to send JSON, use this header:

Content-Type: application/json

这篇关于使用POST发送JSON对象时使用HTTP 415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆