HTTP/1.1 415 Unsupported Media Type

java 专栏收录该内容
47 篇文章 0 订阅
        httpPost.addHeader("encoding", "UTF-8");
        httpPost.addHeader("content-type", "application/json; charset=utf-8");

maven:

<dependency>  
            <groupId>org.apache.httpcomponents</groupId>  
            <artifactId>httpcore</artifactId>  
             <version>4.4.6</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.httpcomponents</groupId>  
            <artifactId>httpclient</artifactId>  
            <version>4.5.3</version>  
        </dependency>
        <dependency>

httpclient:

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         HttpClient client = new DefaultHttpClient();
            String path = "http://10.118.61.31:8888/services/lock/getLockKeyByNo/A0001";
            HttpGet get = new HttpGet(path);
           // get.setHeader("lockNo", "A0001");

            HttpResponse response = null;
            try {
                response = client.execute(get);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            StatusLine status =  response.getStatusLine();
            int statusCode = status.getStatusCode();
            System.out.println(statusCode);
            if(statusCode == HttpStatus.SC_OK){//响应码200
                 HttpEntity entity = response.getEntity();

                try {
                    String  string = EntityUtils.toString(entity, "utf-8");
                    System.out.println(string);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
    }

httppost:

public void post() throws UnsupportedEncodingException, JSONException {  
    // 创建默认的httpClient实例.    
    HttpClient httpClient = HttpClientBuilder.create().build();
    // 创建httppost    
    HttpPost httpPost = new HttpPost("http://10.118.61.80:9999/services/unloadCarTaskRestService/listCurrentPortUnloadCarTaskPost");  

   // NameValuePair[] info={new NameValuePair("str","str33434")};
    Gson gson = new Gson();
    //toJson方法参数即一个javabean。返回值即一个json字符串
    String json = gson.toJson(new G("A0001"));
    JSONObject object = new JSONObject();
    object.put("unloadPort", "X5");
    object.put("taskStatus", "1");
    object.put("transitDepotNo", "000006");
    StringEntity body = new StringEntity(object.toString(1));
    // 创建参数队列    
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
//    formparams.add(new BasicNameValuePair("lockNo", json));  
   // formparams.add(new BasicNameValuePair("transitDepotNo", "45646"));  

    try {  
        //RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
//       UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
        httpPost.setEntity(body);  
        httpPost.addHeader("encoding", "UTF-8");
        httpPost.addHeader("content-type", "application/json; charset=utf-8");
        System.out.println("executing request " + httpPost.getURI());  
        HttpResponse response = httpClient.execute(httpPost);  
        System.out.println(response.getStatusLine());

        try {  
            HttpEntity entity1 = response.getEntity();

            if (entity1 != null) {  
                System.out.println("--------------------------------------");  
                System.out.println("Response content: " + EntityUtils.toString(entity1, "UTF-8"));  
                System.out.println("--------------------------------------");  
            }  
        } finally { 
            httpPost.releaseConnection();
//            response.close();  
        }  
    } catch (ClientProtocolException e) {  
        e.printStackTrace();  
    } catch (UnsupportedEncodingException e1) {  
        e1.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        // 关闭连接,释放资源    
//        try {  
////            httpClient.close();  
//        } catch (IOException e) {  
//            e.printStackTrace();  
//        }  
    }  
}  
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

qq_31443653

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值