【问题标题】:Get content of HTTPS GET request in Android在 Android 中获取 HTTPS GET 请求的内容
【发布时间】:2012-02-20 13:13:38
【问题描述】:

我有以下代码,它接受一个普通的 HTTP GET 请求并将输出的 html 作为字符串返回。

public static String getURLContent(String URL){
        String Result = "";
        String IP = "http://localhost/";
        try {
            // Create a URL for the desired page
            URL url = new URL(IP.concat(URL));

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
                Result = Result+str+"~";
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result;
    }

我想为未签名的 ssl 证书实现同样的事情,但我在 Java 或 Android 编程方面有点新手,发现以前对类似问题的一些回答非常令人困惑。

有人可以更改上面的代码以处理 HTTPS 请求吗?

另一个问题,如果我通过 GET 请求发送未加密的数据并将数据库条目打印到函数返回其内容的网页上,是否会有中间人攻击的风险。使用 POST 请求会更好吗?

我选择使用 SSL 的原因是因为有人告诉我发送的数据是加密的。数据是敏感的,如果我发送类似 localhost/login.php?user=jim&password=sd7vbsksd8 的内容,它将返回“user=jim permission=admin age=23”,这是我不希望其他人看到的数据,如果他们只是使用浏览器并发送相同的请求。

【问题讨论】:

    标签: android ssl https get request


    【解决方案1】:

    试试这个:

    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URI;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    public class TestHttpGet {
        public void executeHttpGet() throws Exception {
            BufferedReader in = null;
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI("http://w3mentor.com/"));
                HttpResponse response = client.execute(request);
                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String page = sb.toString();
                System.out.println(page);
                } finally {
                if (in != null) {
                    try {
                        in.close();
                        } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    我们可以将参数添加到 HTTP Get 请求中

    HttpGet method = new HttpGet("http://w3mentor.com/download.aspx?key=valueGoesHere");
    client.execute(method);
    

    Android 应该自动使用 ssl。也许您在 localhost 上使用的 ssl 证书不受信任?检查这个:Trusting all certificates using HttpClient over HTTPS

    检查您是否可以使用浏览器浏览https://yourhost/login.php?user=jim&password=sd7vbsksd8

    【讨论】:

    • 谢谢,当我使用您提供的链接时,这成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2020-03-26
    相关资源
    最近更新 更多