首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTTP 415,同时使用POST发送JSON对象

HTTP 415,同时使用POST发送JSON对象
EN

Stack Overflow用户
提问于 2012-11-06 15:42:56
回答 2查看 42.6K关注 0票数 20
代码语言:javascript
复制
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对象。上面是代码,但我得到了

代码语言:javascript
复制
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)"

我的代码出了什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-06 15:48:35

您得到的HTTP响应代码是

代码语言:javascript
复制
415 Unsupported Media Type

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

代码语言:javascript
复制
Content-Type: application/x-www-form-urlencoded

这是提交表单时浏览器发送的内容类型。如果您想发送JSON,请使用此header:

代码语言:javascript
复制
Content-Type: application/json
票数 40
EN

Stack Overflow用户

发布于 2017-10-06 04:20:16

我正确地通过了

代码语言:javascript
复制
Content-Type: application/json

但是我的服务器仍然拒绝这个请求,因为我也通过了

代码语言:javascript
复制
Accept: application/json

这在我的情况下是不允许的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13246529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档