用别人的服务 java 发送post 请求 报406错误

public static String post(String url, String token, String no) {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        post.setHeader("Content-Type", "application/json");
       post.addHeader("Authorization", " Basic YWRtaW46");
        String result = "";

        try {
            if (token == null) {
            } else {
                //第一种方式
//                List params = new ArrayList();
//                params.add(new BasicNameValuePair("token", token));
//                params.add(new BasicNameValuePair("sn", no));
//                // 创建form表单对象
//                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
//                formEntity.setContentType("Content-Type:application/json");

//                //第二种方式
                JSONObject jsonObject = new JSONObject();
            //    jsonObject.put("token", token);
                jsonObject.put("sn", no);

                StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
                //s.setContentEncoding("application/json;charset=utf-8");
                post.setEntity(s);

            }


            // 发送请求
            HttpResponse httpResponse = client.execute(post);

            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            inStream.close();

            result = strber.toString();
            System.out.println(result);

            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                System.out.println("请求服务器成功,做相应处理");

            } else {

                System.out.println("请求服务端失败");

            }


        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }

        return result;
    }

发送完成请求后 系统 返回406

    post.addHeader("Authorization", " Basic YWRtaW46");

去掉
对方系统 Authorization 的值要放 token

你可能感兴趣的:(笔记)