java - HttpURLConnection - 使用基本身份验证的重复请求

标签 java android rest httpurlconnection basic-authentication

我的应用需要向同一个域发出大量请求,以获得具有基本身份验证的 REST API。

下面的代码有效,但每次都需要用户名和密码。有没有办法像在浏览器中一样只进行一次身份验证? (我在桌面版的 c# 中遇到了同样的问题)

public class RestRequest {

    public enum RestMethod {GET, POST}

    private String mUrl, mParams;
    private RestMethod mMethod;

    public RestRequest(RestMethod method, String url) {
        this(method, url, "");
    }

    public RestRequest(RestMethod method, String url, String params) {
        mUrl = url;
        mParams = (params != null) ? params : "";
        mMethod = method;
    }


    public RestResponse sendRequest(String authorization) throws IOException {
        HttpURLConnection connection = openConnection(authorization);
        if (mMethod == RestMethod.POST) {
            postParams(connection);
        }

        InputStream is = null;
        boolean error = false;
        int statusCode = HttpURLConnection.HTTP_ACCEPTED;

        try {
            is = connection.getInputStream();
        } catch (IOException e) {
            statusCode = getErrorCode(connection);
            is = connection.getErrorStream();
        }

        return new RestResponse(readStream(is), error, statusCode);
    }

    public RestRequest addParam(String name, String value) {
        if (mMethod == RestMethod.GET) {
            try {
                String encoded = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
                mParams += name + "=" + encoded + "&";
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return this;
    }

    //authorization is username:password
    private HttpURLConnection openConnection(String authorization) throws IOException {
        URLConnection connection;
        URL url;
        if (mMethod == RestMethod.GET)
            url = new URL(mUrl + "?" + mParams);
        else
            url = new URL(mUrl);

        connection = url.openConnection();
        String authStringEnc = Base64.encodeToString(authorization.getBytes(), Base64.NO_WRAP);
        connection.setRequestProperty("Authorization", "Basic " + authStringEnc);

        return (HttpsURLConnection) connection;
    }

    private void postParams(URLConnection connection) throws IOException {
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");

        OutputStream output = connection.getOutputStream();
        output.write(mParams.getBytes());
    }

    private static int getErrorCode(HttpURLConnection conn) {
        int httpStatus;
        try {
            return conn.getResponseCode();
        } catch (IOException e) {
            return -1;
        }
    }

    private String readStream(InputStream is) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            if (is != null) {
                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}

谢谢。

这部分在这里是因为我的问题“主要是代码”。

最佳答案

浏览器使用 Cookie。为此,您需要拦截登录响应中的 cookie,保存它们,稍后您可以在请求中发送它们。 Look at the answer on this stackoverflow answer on how to achieve it

关于java - HttpURLConnection - 使用基本身份验证的重复请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725162/

相关文章:

java - Android中的分页文本

android - Fragment 中的自定义 ListView 不起作用

rest - TFS REST API 在分支上获得最新的成功构建

java - Apache Tomcat 9.0 端口已在使用中

java - 使用 Selenium Webdriver 和 HtmlUnit 下载网页内容

Android API 11 在应用程序小部件中引入了新组件,包括 ListView 和 GridView,现在有没有办法在应用程序小部件中水平滚动?

mysql - 如何在restful api mysql中请求多个字段

php - 来自 WikiMedia API 的 cURL HTTP 请求不起作用

java - XmlPullparser 从 url 解析 xml 不起作用

java - 如何设置文本格式?