Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpUrlConnection 403禁止的错误_Java_Android_Httpurlconnection - Fatal编程技术网

Java HttpUrlConnection 403禁止的错误

Java HttpUrlConnection 403禁止的错误,java,android,httpurlconnection,Java,Android,Httpurlconnection,下面是我的代码片段。错误来了。请任何人给出克服这个403错误的解决方案。这是post JSON请求 @Override protected String doInBackground(Void... voids) { try { trustAllHosts(); URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection)

下面是我的代码片段。错误来了。请任何人给出克服这个403错误的解决方案。这是post JSON请求

@Override
protected String doInBackground(Void... voids) {

        try {
            trustAllHosts();
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            String userCredentials = username+":"+password;
            String basicAuth = "Basic " + new String(android.util.Base64.encode(userCredentials.getBytes(), Base64.DEFAULT));
            conn.setRequestProperty ("Authorization", basicAuth);
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            conn.setRequestProperty("Accept","application/json");
            conn.setRequestProperty("X-CSRF-TOKEN",token);
            conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:221.0) Gecko/20100101 Firefox/31.0");


            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            PostRootObject postRootObject = new PostRootObject();



            Gson gson = new GsonBuilder().create();
            String json = gson.toJson(postRootObject);

            JSONObject jsonObject = new JSONObject(json);

            Log.e("Json object",""+jsonObject);

            DataOutputStream os = new 
            DataOutputStream(conn.getOutputStream());

//                os.writeBytes(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
            os.writeBytes(jsonObject.toString());

            os.flush();


            int responseCode=conn.getResponseCode();


            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response="";

            }

            Log.i("STATUS", String.valueOf(conn.getResponseCode()));
            Log.i("MSG" , conn.getResponseMessage());
            os.close();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

可能您的清单文件中缺少这一行:

 <uses-permission android:name="android.permission.INTERNET" />


您是否尝试过使用邮递员投递?也许你的auth方法期望的输入有点不同,与邮递员配合得很好。仅代码有问题:(httpurlconnection.getErrorStream()抛出“缓冲区(com.android.okhttp.internal.http.HttpConnection$FixedLengthSource@91b93f2).inputStream()此错误。Rafael-抱歉,我在发布时删除了原始url,并将其替换为url。现在更新了帖子。这不是url错误。抱歉,我在1分钟后看到,您有解决方案。我还授予了internet权限。此外,我没有解决方案。正在等待有人给出解决方案