当我有一个带有代理“IP:端口”的服务器时,如何在我的 Android 应用程序上连接到互联网并要求输入用户名和密码

发布于 2024-12-20 18:21:35 字数 190 浏览 1 评论 0原文

我正在android上开发一个应用程序,公司中有一个代理服务器阻止它访问互联网。
我记得java中有Http方法可以达到这个目的,我搜索了它们但没有结果。

所以我想要的是如何放置服务器 IP 地址和端口号、我的登录用户名和密码以及域名。

注意:我已将模拟器设置为通过 APNs 连接到互联网,并且它通过浏览器成功连接到互联网。

I am developing an application on android, and there is a proxy server in the company preventing it from accessing the internet.
I recall there are Http methods in java that serves the purpose, I searched for them but with no result.

So what I'd like is how to put the server IP-address and port-number, with my login-username and password, and the domain-name.

Note: I have set my emulator to connect to the internet by going to the APNs and it successfully connects to the internet via the browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-12-27 18:21:35

尝试使用以下代码

/**
 * A simple example that uses HttpClient to execute an HTTP request
 * over a secure connection tunneled through an authenticating proxy.
 */
public class ClientProxyAuthentication {

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

        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("localhost", 8080),
                    new UsernamePasswordCredentials("username", "password"));

            HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
            HttpHost proxy = new HttpHost("localhost", 8080);

            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            HttpGet httpget = new HttpGet("/");

            System.out.println("executing request: " + httpget.getRequestLine());
            System.out.println("via proxy: " + proxy);
            System.out.println("to target: " + targetHost);

            HttpResponse response = httpclient.execute(targetHost, httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}

try to use the following code

/**
 * A simple example that uses HttpClient to execute an HTTP request
 * over a secure connection tunneled through an authenticating proxy.
 */
public class ClientProxyAuthentication {

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

        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("localhost", 8080),
                    new UsernamePasswordCredentials("username", "password"));

            HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
            HttpHost proxy = new HttpHost("localhost", 8080);

            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            HttpGet httpget = new HttpGet("/");

            System.out.println("executing request: " + httpget.getRequestLine());
            System.out.println("via proxy: " + proxy);
            System.out.println("to target: " + targetHost);

            HttpResponse response = httpclient.execute(targetHost, httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文