【问题标题】:403 error in Java Application but not in browser or PostmanJava 应用程序中的 403 错误,但在浏览器或 Postman 中没有
【发布时间】:2017-05-08 09:54:51
【问题描述】:

我有一个在服务器上执行各种 HTTP 请求的 java 程序。

我的 GET 返回 403 错误。

我可以通过浏览器访问相同的 url,但系统会提示我添加安全例外。最初,当我在 Postman 中执行 GET 时,我也遇到了 403 错误,但在删除身份验证密钥后,它可以工作。我检查了我的 Java 应用程序中的标题,它似乎与 Postman 中的相同。我还能做些什么来解决我的 Java 应用程序的这个问题吗?

这是我的代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.codec.binary.Base64;

public class Rest{
private static final String USER_AGENT =  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
//private static final String USER_AGENT = "Mozilla/5.0";
private static String name = "admin";
private static String password = "password";

private static String authString = name + ":" + password;
private static byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
private static String authStringEnc = new String(authEncBytes);

private static String GET_URL = "https://127.0.0.1:8080/storage";
private static String PUT_URL_PREFIX = "https://127.0.0.1:8080/storage/";
private static String PUT_URL = "";
private static String DELETE_URL_PREFIX = "https://127.0.0.1:8080/storage/";
private static String  DELETE_URL = "";
private static String HEAD_URL = "https://127.0.0.1:8080/storage";

private static int deleteCount = 10;
private static int getCount = 10;
private static int headCount = 10;
private static int putCount = 10;

private static int deleteBytes = 0;
private static int getBytes = 0;
private static int headBytes = 0;
private static int putBytes = 0;

private static char[] data = new char[20];

public static void main(String args[]) throws IOException{


    TrustManager[] trustAllCerts = new TrustManager[]{
        new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers(){
                return null;
            }
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType){
                //No need to implement.
            }
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType){
                //No need to implement.
            }
        }
    };

    // Install the all-trusting trust manager
    try 
    {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        disableSSL();

        for(int i = 0; i < putCount; i++){
            PUT_URL = PUT_URL_PREFIX + i;
            System.out.println("PUT URL: " + PUT_URL);
            sendPUT();
            System.out.println("PUT Done");
            putBytes += data.length;
        }

        for(int i = 0; i < getCount; i++){
            sendGET();
            System.out.println("GET Done");
        }

        for(int i = 0; i < deleteCount; i++){
            DELETE_URL = DELETE_URL_PREFIX + i;
            System.out.println("DELETE URL: " + DELETE_URL);
            sendDELETE();
            System.out.println("DELETE Done");
        }

        for(int i = 0; i < headCount; i++){
            sendHEAD();
            System.out.println("HEAD Done");
        }

        System.out.println("\nPUT operation count = " + putCount + "\nPUT Bandwidth = " + putBytes);
        System.out.println();
        System.out.println("\nGET operation count = " + getCount + "\nGET Bandwidth = " + getBytes);
        System.out.println();
        System.out.println("\nDELETE operation count = " + deleteCount + "\nDELETE Bandwidth = " + deleteBytes);
        System.out.println();
        System.out.println("\nHEAD operation count = " + headCount + "\nHEAD Bandwidth = " + headBytes);
        System.out.println();

    } 
    catch (Exception e) 
    {
        System.out.println(e);
    }
}

public static void sendGET() throws IOException{

    URL obj = new URL(GET_URL);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    con.setRequestProperty("Authorization", "Basic " + authStringEnc);
    con.setRequestMethod("GET");

    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-Type", "application/xml");
    int responseCode = con.getResponseCode();
    System.out.println("GET Response Code :: " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) { // success
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append("\n" + inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("GET request didn't work");
    }
}

private static void sendPUT() throws IOException {
    Arrays.fill(data, '1');
    String str = new String(data);
    URL obj = new URL(PUT_URL);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    con.setRequestProperty("Authorization", "Basic " + authStringEnc);
    con.setRequestMethod("PUT");
    con.setRequestProperty("User-Agent", USER_AGENT);

    // For PUT only - START
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(str);
    wr.flush();
    wr.close();
    // For PUT only - END

    int responseCode = con.getResponseCode();
    System.out.println("PUT Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("PUT request didn't work");
    }

}

private static void sendDELETE() throws IOException{

    URL obj = new URL(DELETE_URL);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestMethod("DELETE");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setDoOutput(true);

    int responseCode = con.getResponseCode();
    System.out.println("DELETE Response Code :: " + responseCode);

}

public static void sendHEAD() throws IOException{

    URL obj = new URL(HEAD_URL);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    con.setRequestMethod("HEAD");

    con.setRequestProperty("User-Agent", USER_AGENT);
    int responseCode = con.getResponseCode();
    System.out.println("HEAD Response Code :: " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) { // success

        Map<String, List<String>> map = con.getHeaderFields();


        System.out.println("Printing All Response Header for URL: " + obj.toString() + "\n");
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        System.out.println("\nGet Response Header By Key ...\n");
        List<String> contentLength = map.get("Content-Length");
        if (contentLength == null) {
            System.out.println("'Content-Length' doesn't present in Header!");
        } else {
            for (String header : contentLength) {
                System.out.println("Content-Lenght: " + header);
            }
        }
    } else {
        System.out.println("HEAD request didn't work");
    }
}
private static void disableSSL() {
      try {
         TrustManager[] trustAllCerts = new TrustManager[] { new   MyTrustManager() };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

【问题讨论】:

  • 禁止?表示您无权访问 url 或路径设置不正确。分享您的代码示例。
  • 啊,我的密码错误。得到了 GET 工作并且还有一个 PUT 和 HEAD 返回一个 200 响应。我有一个 DELETE 仍然返回 403 响应。不过,删除在 Postman 中使用相同的凭据和 URL。关于可能导致此问题的任何提示?从我目前的研究来看,我看不出有什么问题。
  • 再次可能与类似问题有关。您可以使用 DUMMY 服务器/占位符凭据共享您的代码吗?

标签: java get http-status-code-403


【解决方案1】:

我的密码错误。一旦我用正确的密码更新了它,我就可以执行 GET。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 2016-07-11
    • 2019-12-20
    • 2011-01-30
    • 2012-01-11
    相关资源
    最近更新 更多