HTTP/1.1 415 Unsupported Media Type

        httpPost.addHeader("encoding", "UTF-8");
        httpPost.addHeader("content-type", "application/json; charset=utf-8");

maven:

<dependency>  
            <groupId>org.apache.httpcomponentsgroupId>  
            <artifactId>httpcoreartifactId>  
             <version>4.4.6version>  
        dependency>  
        <dependency>  
            <groupId>org.apache.httpcomponentsgroupId>  
            <artifactId>httpclientartifactId>  
            <version>4.5.3version>  
        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 formparams = new ArrayList();  
//    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();  
//        }  
    }  
}  

你可能感兴趣的:(java)