Главная»Core Java»How Do I Share a HttpURLConnection Sessions across Multiple Requests
How Do I Share a HttpURLConnection Sessions across Multiple Requests
Автор adminНа чтение 5 минПросмотров733Опубликовано
HttpURLConnection is subclass of URLConnection with support for HTTP-specific features, for example: open a connection, retrieve message and post data to server.
First of all, we need declare a global variable cookie:
publicstaticString cookie = null;
Secondly, create a method GetCookieFromURL, open a URL connection, read the cookie from opened connection and assign to global variable cookie:
Please note the connection will be closed when method is finished(it’s one-off connection).
Third, we will open a different HTTP connection with cookies we got previously, prior to opening collection, use urlConn.setRequestProperty set the cookie value, by this way, we are able to pass-by session across multiple requests, it’s very useful, for example, after login, we need use login session for a while to read and modify data.