首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >确保HTTPS连接已正确设置

确保HTTPS连接已正确设置
EN

Stack Overflow用户
提问于 2018-06-04 05:29:21
回答 1查看 54关注 0票数 1

根据Android Documentation的说法

假设您有一台web服务器,其中包含由知名CA颁发的证书,您可以使用如下代码发出安全请求:

代码语言:javascript
复制
URL url = new URL("https://wikipedia.org");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);

是的,它真的可以这么简单。

我目前正在做一个安卓项目,现在想开始介绍一个用户系统(用户/密码的DataBase,注册/登录功能,等等)。

下面是我们目前处理连接的方式(在项目的Android部分):

代码语言:javascript
复制
package com.example.payne.simpletestapp;


import org.osmdroid.util.BoundingBox;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class ServerConnection {

    public String serverAddress;
    public int port;

    public ServerConnection(String addr, int port){
        this.serverAddress = addr;
        this.port = port;

    }

    public ServerConnection(String addr){

        this.port = 80;
        this.serverAddress = addr;

    }

    /**
     * Envoie une requête au serveur de façon périodique.
     * <p></p>
     * Reçoit toutes les alertes sur le territoire
     *
     * @return
     */
    public String ping(BoundingBox boundingBox) throws Exception {

        String param =
            "?nord=" + boundingBox.getLatNorth() +
            "&sud=" + boundingBox.getLatSouth() +
            "&est=" + boundingBox.getLonEast() +
            "&ouest=" + boundingBox.getLonWest();

        return this.getRequest("/alertes", param);

    }


    public String getRequest(final String path, final String param) throws Exception {

        final Response result = new Response();

        Thread connection = new Thread(new Runnable() {

            @Override
            public void run() {

                try{
                    URL obj = new URL(serverAddress + path + param);
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                    // optional default is GET
                    con.setRequestMethod("GET");
                    // int responseCode = con.getResponseCode();

                    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();

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

                    in.close();

                    result.response = response.toString();

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        connection.start();
        connection.join();


        return result.response;

    }

    public Boolean postAlert(Alerte alerte) {

        boolean success = false;
        alerte.log();

        String param =  "?type=" + alerte.type +
                        "&lat=" + alerte.getLatitude() +
                        "&lng=" + alerte.getLongitude();

        try {
            this.getRequest("/putAlert", param);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return success;

    }


}

注意,在其他地方,团队的另一个成员似乎一直在以不同的方式建立连接(在项目的“后端”部分,该部分由服务器使用,而不一定由Android使用):

代码语言:javascript
复制
private String getRssFeed() {
    try {
        String rss = "";
        URL rssSource = new URL("https://anURLwithHTTPS.com/");
        URLConnection rssSrc = rssSource.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        rssSrc.getInputStream(), StandardCharsets.UTF_8));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            rss += inputLine;
        }

        in.close();

        String rssCleaned = rss.replaceAll("&lt;", "<").replaceAll("&gt;", ">").substring(564);

        return rssCleaned;
    } catch (MalformedURLException ex) {
        Logger.getLogger(AlertesFluxRss.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(AlertesFluxRss.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

问题是:

第一个代码(与安卓相关的代码)是否会对第二个代码使用加密的连接(HTTPS)?

  • What?

  • 我们如何测试发送给HttpURLConnection的信息是否是encrypted?

  • Is投射到HttpURLConnection的威胁?文档似乎表明它将自动重新转换为"HttpsURLConnection".

  • Since HttpsURLConnection extends,它具有HttpURLConnection的所有功能以及更多功能,对吧?

我确实找到了this,但它不是我要找的。

我们目前注册新用户的想法是通过en加密连接(沿着https://myWebsite.com/api/?user=Bob&pass=amazing的路线)将他们的信息发送到服务器(托管在https://www.heroku.com上)。

如果有更好的方法做这样的事情,请让我知道。我们将很乐意考虑这些建议。当涉及到库时,我也没有太多的知识,所以请分享你所知道的任何可能使我的方法更安全和更快实现的东西。

非常感谢您提前给予我们的帮助!:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 07:21:25

提供的第一个代码(与安卓相关的代码)会使用加密连接(HTTPS)吗?

如果没有看到“第一个代码”使用的URL,就不会显示出来。在您引用的Android示例中,URL字符串"https://wikipedia.org"根据定义保证了这一点。如果它以https:开头,它就会使用加密的HTTPS;否则,它就不会,也不会。

第二个呢?

是的,基于上面给出的原因。

如何测试发送的信息是否加密?

你不需要。你使用的是一个https:网址。其余的都是自动完成的。它起作用了。工作了22年。不需要测试。不要测试平台。如果您必须检查它,请尝试将类型转换为HttpsURLConnection。如果是HTTPS,就会成功,否则就会失败。

是在向HttpURLConnection施放威胁?

不是的。我不明白你为什么这么问。

文档似乎表明它会自动重新转换为"HttpsURLConnection“。

不,它不会。它表示已经创建了一个HttpsURLConnection。没有选角。

由于HttpsURLConnection扩展了HttpURLConnection,所以它具有HttpURLConnection的所有功能,甚至更多,对吧?

正确的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档