首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Android上使用Cookie发出http请求?

在Android上使用Cookie发出HTTP请求,可以使用Java的内置库java.net.HttpURLConnection和java.net.CookieManager。以下是一个简单的示例代码:

代码语言:java
复制
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;

public class CookieExample {
    public static void main(String[] args) {
        try {
            // 初始化CookieManager
            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);

            // 设置请求URL
            URL url = new URL("https://example.com/login");

            // 创建HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            // 添加请求参数
            String postData = "username=your_username&password=your_password";
            connection.getOutputStream().write(postData.getBytes());

            // 执行请求
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应内容
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

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

                // 打印响应内容
                System.out.println(response.toString());

                // 设置请求URL
                URL requestUrl = new URL("https://example.com/request");

                // 创建HttpURLConnection
                HttpURLConnection requestConnection = (HttpURLConnection) requestUrl.openConnection();
                requestConnection.setRequestMethod("GET");

                // 执行请求
                int requestResponseCode = requestConnection.getResponseCode();
                if (requestResponseCode == HttpURLConnection.HTTP_OK) {
                    // 读取响应内容
                    BufferedReader requestIn = new BufferedReader(new InputStreamReader(requestConnection.getInputStream()));
                    String requestInputLine;
                    StringBuffer requestResponse = new StringBuffer();

                    while ((requestInputLine = requestIn.readLine()) != null) {
                        requestResponse.append(requestInputLine);
                    }
                    requestIn.close();

                    // 打印响应内容
                    System.out.println(requestResponse.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这个示例中,我们首先初始化了一个CookieManager实例,并将其设置为默认的CookieHandler。然后,我们创建了一个HttpURLConnection实例,并使用POST方法发送登录请求。在登录成功后,我们再次创建了一个HttpURLConnection实例,并使用GET方法发送请求。由于我们已经设置了CookieManager,因此它会自动处理Cookie,并在请求中添加相应的Cookie。

注意:在实际应用中,请确保使用安全的连接(如HTTPS)来保护Cookie和敏感数据的安全。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

开发:随笔记录之 HTTP 调用

public class HttpUtil { static Logger log = Logger.getLogger(HttpUtil.class); public static String send(String callURL,String postData) throws Exception { log.info("call url is:" + callURL); log.info("call postData is:" + postData); try { URL url = new URL(callURL); HttpURLConnection connection = null; connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); DataOutputStream out = new DataOutputStream(connection .getOutputStream()); out.write(postData.getBytes("UTF-8")); out.flush(); out.close(); int rc = connection.getResponseCode(); log.info("connect result is:" + rc); // 响应成功 if (rc == 200) { String temp; InputStream in = null; in = connection.getInputStream(); BufferedReader data = new BufferedReader(new InputStreamReader( in, "utf-8")); StringBuffer result = new StringBuffer(); while ((temp = data.readLine()) != null) { result.append(temp); temp = null; } data.close(); in.close(); log.info("returnData is:" + result.toString()); return result.toString(); } } catch (IOException io) { log.error(io.toString()); throw io; } catch (Exception e) { log.error(e.getMessage()); throw e; } return null;

03
领券