{"id":306144,"date":"2020-06-29T21:01:10","date_gmt":"2020-06-29T21:01:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=306144"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=306144","title":{"rendered":"\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a session \u0432 Java \u0438 Python. HttpURLConnection \u0438 CookieManager (Java). Requests(Python)"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/508674\/\">\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u044e. \u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<ol>\n<li>\u041d\u0430 \u0430\u0434\u0440\u0435\u0441 <a href=\"http:\/\/login\" rel=\"nofollow\">http:\/\/localhost:8080\/login<\/a> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0443\u0441\u0442\u043e\u0439 GET \u0437\u0430\u043f\u0440\u043e\u0441.<\/li>\n<li>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 \u0444\u043e\u0440\u043c\u043e\u0447\u043a\u0443 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 Cookie \u0432\u0438\u0434\u0430 \u00abJSESSIONID=094BC0A489335CF8EE58C8E7846FE49B\u00bb.<\/li>\n<li>\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0432 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c, \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f POST \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u043d\u0435\u0435 Cookie, \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u00abusername=Fox&amp;password=123\u00bb. \u0412 Headers \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u00abContent-Type: application\/x-www-form-urlencoded\u00bb.<\/li>\n<li>\u0412 \u043e\u0442\u0432\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430\u043c \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e cookie c \u043d\u043e\u0432\u044b\u043c \u00abJSESSIONID=\u00bb. \u0421\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0435\u0440\u0435\u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f \u043d\u0430 <a href=\"http:\/\/login\" rel=\"nofollow\">http:\/\/localhost:8080\/<\/a> \u043f\u0443\u0442\u0451\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043d\u043e\u0432\u043e\u0439 Cookie.<\/li>\n<li>\u0414\u0430\u043b\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 API \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 Cookie \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/li>\n<\/ol>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 Java \u0438 \u043d\u0430 Python. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/7o\/zt\/y2\/7ozty2bizbe1webfbfatajxefhi.jpeg\"><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435:<\/h4>\n<p>  <\/p>\n<ul>\n<li> <a href=\"#section1\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Python. Requests.<\/a> <\/li>\n<li> <a href=\"#section2\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Java, HttpURLConnection \u0438 CookieManager.<\/a> <\/li>\n<li> <a href=\"#section3\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Java, HttpURLConnection \u0431\u0435\u0437 CookieManager.<\/a><\/li>\n<\/ul>\n<p>  <a name=\"section1\"><\/a><\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Python. Requests.<\/h3>\n<p>  \u041f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e \u043d\u0430 Python \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0441\u0430\u0439\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u0430\u043c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/requests.readthedocs.io\/en\/master\/\" rel=\"nofollow\">requests <\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u043f\u0440\u0430\u0432\u0434\u044b\u0432\u0430\u0435\u0442 \u0441\u0432\u043e\u0439 \u043b\u043e\u0437\u0443\u043d\u0433: <\/p>\n<blockquote><p><i>HTTP for Humans<\/i><\/p><\/blockquote>\n<p>  \u0412\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c:  <\/p>\n<pre><code class=\"python\">import requests session = requests.session()  #\u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0435\u0441\u0441\u0438\u044e url = &quot;http:\/\/localhost:8080\/login&quot; session.get(url)   #\u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c cookie data = {&quot;username&quot;: &quot;Fox&quot;, &quot;password&quot;: &quot;123&quot;}  response = session.post(url, data=data) #\u043b\u043e\u0433\u0438\u043d\u0438\u043c\u0441\u044f <\/code><\/pre>\n<p>  \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043c\u0430\u0445\u0438\u043d\u0430\u0446\u0438\u0438 \u0441 Cookie \u0438 \u043f\u0435\u0440\u0435\u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u043f\u0440\u044f\u043c\u043e \u043a\u0430\u043a \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u0422\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u0432\u0435\u0441\u0442\u0438 \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <i>session2<\/i>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0441\u0440\u0430\u0437\u0443 \u0434\u0432\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  <a name=\"section2\"><\/a><\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Java, HttpURLConnection \u0438 CookieManager.<\/h3>\n<p>  \u041f\u043e\u0438\u0441\u043a\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e \u043d\u0430 Java \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u0441\u0440\u0430\u0437\u0443 \u043a \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/net\/package-summary.html\" rel=\"nofollow\">java.net<\/a>, <a href=\"http:\/\/hc.apache.org\/httpcomponents-client-ga\/httpclient\/apidocs\/org\/apache\/http\/client\/package-summary.html\" rel=\"nofollow\">Apache HttpClient<\/a> \u0438 <a href=\"https:\/\/square.github.io\/okhttp\/3.x\/okhttp\/\" rel=\"nofollow\">OkHttp3<\/a>. <\/p>\n<p>  \u042f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u043d\u0430 HttpURLConnection (java.net). \u041f\u043b\u044e\u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 &quot;<i>\u0438\u0437-\u043f\u043e\u0434 \u043a\u043e\u0440\u043e\u0431\u043a\u0438<\/i>&quot;, \u0430 \u0442\u0430\u043a \u0436\u0435, \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0434 android, \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435 \u0435\u0441\u0442\u044c <a href=\"https:\/\/developer.android.com\/reference\/java\/net\/HttpURLConnection\" rel=\"nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a>. \u041c\u0438\u043d\u0443\u0441\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u044a\u0451\u043c \u043a\u043e\u0434\u0430. (\u041f\u043e\u0441\u043b\u0435 Python \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u043e\u043b\u044c).<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0451\u043c. \u041f\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0441\u0441\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/developer.android.com\/reference\/android\/webkit\/CookieManager?hl=ru\" rel=\"nofollow\">CookieManager<\/a>:<\/p>\n<pre><code class=\"java\">CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434:  <\/p>\n<ul>\n<li> \u00abCookiePolicy.ACCEPT_ALL\u00bb \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0432\u0441\u0435\u043c\u0438 cookie.<\/li>\n<li> \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f cookieManager \u0434\u0430\u043b\u0435\u0435 \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u041e\u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u0442 \u0432\u0441\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0438, \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u044d\u0442\u043e\u0439 \u043e\u0434\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0440\u0443\u043a\u0430\u043c\u0438 \u043c\u0435\u043d\u044f\u0442\u044c Cookie<\/li>\n<\/ul>\n<p>  \u0423\u0447\u0442\u044f \u044d\u0442\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443:  <\/p>\n<pre><code class=\"java\"> CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); <\/code><\/pre>\n<p>  \u041f\u0443\u043d\u043a\u0442 1 \u0438 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0439 Cookie  <\/p>\n<pre><code class=\"java\">URL url = new URL(&quot;http:\/\/localhost:8080\/login&quot;); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(&quot;GET&quot;); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; final StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) {     content.append(inputLine); } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0448 cookieManager \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c Cookie \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0442 \u0435\u0451 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. <\/p>\n<p>  \u0412\u0435\u0441\u0435\u043b\u044c\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 POST \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c.   <\/p>\n<pre><code class=\"java\">url = new URL(&quot;http:\/\/localhost:8080\/login&quot;); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(&quot;POST&quot;); <\/code><\/pre>\n<p>  \u041d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 Headers \u00abContent-Type: application\/x-www-form-urlencoded\u00bb. <br \/>  \u041f\u043e\u0447\u0435\u043c\u0443 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f setRequestProperty, \u0430 \u043d\u0435 setHeaders (\u0438\u043b\u0438 addHeaders) \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 getHeaderField, \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0437\u0430\u0433\u0430\u0434\u043a\u043e\u0439.   <\/p>\n<pre><code class=\"java\">con.setRequestProperty(&quot;Content-Type&quot;, &quot;application\/x-www-form-urlencoded&quot;); <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0451\u0442 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e \u043f\u043e \u043a\u0430\u043a\u0438\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043d\u0435 \u0437\u0430\u0441\u0443\u043d\u0443\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.  <\/p>\n<pre><code class=\"java\">con.setDoOutput(true); <\/code><\/pre>\n<p>  \u041d\u0443\u0436\u043d\u0430 \u044d\u0442\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0417\u0430\u0431\u0430\u0432\u043d\u043e, \u0447\u0442\u043e \u0431\u0435\u0437 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:  <\/p>\n<blockquote><p>Exception in thread \u00abmain\u00bb java.net.ProtocolException: cannot write to a URLConnection if doOutput=false \u2014 call setDoOutput(true)  <\/p><\/blockquote>\n<p>  \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0442\u0443\u0434\u0430 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c:  <\/p>\n<pre><code class=\"java\">final DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeBytes(&quot;username=Fox&amp;password=123&quot;); out.flush(); out.close(); <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0441 \u0443\u0436\u0435 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <a name=\"section3\"><\/a><\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Java, HttpURLConnection \u0431\u0435\u0437 CookieManager.<\/h3>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0431\u0435\u0437 CookieManager \u0438 \u0441\u0430\u043c\u043e\u043c\u0443 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 cookie.<br \/>  \u041f\u0443\u043d\u043a\u0442 1 \u0438 2. \u0412\u044b\u043d\u0438\u043c\u0430\u0435\u043c cookie.  <\/p>\n<pre><code class=\"java\">URL url = new URL(&quot;http:\/\/localhost:8080\/login&quot;); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(&quot;GET&quot;); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; final StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) {     content.append(inputLine); String cookie = con.getHeaderField(&quot;Set-Cookie&quot;).split(&quot;;&quot;)[0]; } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c POST \u0437\u0430\u043f\u0440\u043e\u0441, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0432\u0441\u0442\u0430\u0432\u0438\u0432 cookie \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0432 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0442.\u043a. \u043f\u0435\u0440\u0435\u0434 \u043d\u0438\u043c \u043d\u0430\u0434\u043e \u0443\u0441\u043f\u0435\u0442\u044c \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u043d\u043e\u0432\u043e\u0435 cookie:<\/p>\n<pre><code class=\"java\">\/\/ \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0437\u0430\u043f\u0440\u043e\u0441 url = new URL(&quot;http:\/\/localhost:8080\/login&quot;); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(&quot;POST&quot;); \/\/\u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c headers \u0438 cookie con.setRequestProperty(&quot;Content-Type&quot;, &quot;application\/x-www-form-urlencoded&quot;); con.setRequestProperty(&quot;Cookie&quot;, cookie); \/\/\u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044e con.setInstanceFollowRedirects(false); \/\/\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c con.setDoOutput(true); final DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeBytes(&quot;username=Fox&amp;password=123&quot;); out.flush(); out.close(); \/\/\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0442\u043e\u0440\u043e\u0435 cookie BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; final StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) {     content.append(inputLine); String cookie2 = con.getHeaderField(&quot;Set-Cookie&quot;).split(&quot;;&quot;)[0]; <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0432\u043e \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443:  <\/p>\n<pre><code class=\"java\">con.setRequestProperty(&quot;Cookie&quot;, cookie2); <\/code><\/pre>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0431\u044b\u043b\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u0412 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u043e\u043f\u0440\u043e\u0449\u0435.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/508674\/\"> https:\/\/habr.com\/ru\/post\/508674\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/508674\/\">\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u044e. \u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<ol>\n<li>\u041d\u0430 \u0430\u0434\u0440\u0435\u0441 <a href=\"http:\/\/login\" rel=\"nofollow\">http:\/\/localhost:8080\/login<\/a> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0443\u0441\u0442\u043e\u0439 GET \u0437\u0430\u043f\u0440\u043e\u0441.<\/li>\n<li>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 \u0444\u043e\u0440\u043c\u043e\u0447\u043a\u0443 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 Cookie \u0432\u0438\u0434\u0430 \u00abJSESSIONID=094BC0A489335CF8EE58C8E7846FE49B\u00bb.<\/li>\n<li>\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0432 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c, \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f POST \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u043d\u0435\u0435 Cookie, \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u00abusername=Fox&amp;password=123\u00bb. \u0412 Headers \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u00abContent-Type: application\/x-www-form-urlencoded\u00bb.<\/li>\n<li>\u0412 \u043e\u0442\u0432\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430\u043c \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e cookie c \u043d\u043e\u0432\u044b\u043c \u00abJSESSIONID=\u00bb. \u0421\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0435\u0440\u0435\u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f \u043d\u0430 <a href=\"http:\/\/login\" rel=\"nofollow\">http:\/\/localhost:8080\/<\/a> \u043f\u0443\u0442\u0451\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043d\u043e\u0432\u043e\u0439 Cookie.<\/li>\n<li>\u0414\u0430\u043b\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 API \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 Cookie \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/li>\n<\/ol>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 Java \u0438 \u043d\u0430 Python. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/7o\/zt\/y2\/7ozty2bizbe1webfbfatajxefhi.jpeg\">  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-306144","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306144"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306144\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}