1.The response body must be closed.

In the official document

Each response body is backed by a limited resource like a socket (live network responses) or an open file (for cached responses). Failing to close the response body will leak resources and may ultimately cause the application to slow down or crash.

Both this class and Response implement Closeable. Closing a response simply closes its response body. If you invoke Call.execute or implement Callback.onResponse you must close this body by calling any of the following methods:

Response.close()
Response.body().close()
Response.body().source().close()
Response.body().charStream().close()
Response.body().byteStream().close()
Response.body().bytes()
Response.body().string()
Copy the code

In addition to calling the bytes() and string() methods of response, the response body must be displayed by calling the close method

The following method is recommended to close the Response body

// The synchronous call closes the Response Body
Call call = client.newCall(request);
try (Response response = call.execute()) {
... // Use the response.
}

// An asynchronous call closes the Response Body
Call call = client.newCall(request);
call.enqueue(new Callback() {
  public void onResponse(Call call, Response response) throws IOException {
    try (ResponseBody responseBody = response.body()) {
    ... // Use the response.}}public void onFailure(Call call, IOException e) {...// Handle the failure.}});Copy the code

2. Disable OkHttp automatic redirection

final OkHttpClient client = new OkHttpClient().newBuilder()
                // Disable OkHttp redirects and handle redirects ourselves
                .followRedirects(false)
                .followSslRedirects(false)
                .cookieJar(new LocalCookieJar())
                .build();
Copy the code

3. When OkHttp sends a request after receiving a response, it automatically carries a Cookie

reference

final OkHttpClient client = new OkHttpClient().newBuilder()
                // Disable OkHttp redirects and handle redirects ourselves
                .followRedirects(false)  
                .followSslRedirects(false)
                // Set automatic Cookie carrying for OkHttp
                .cookieJar(new LocalCookieJar())   
                .build();

// The CookieJar is used to store cookies
class LocalCookieJar implements CookieJar{
    List<Cookie> cookies;
    @Override
    public List<Cookie> loadForRequest(HttpUrl arg0) {
         if(cookies ! =null)
                return cookies;
            return new ArrayList<Cookie>();
    }

    @Override
    public void saveFromResponse(HttpUrl arg0, List<Cookie> cookies) {
        this.cookies = cookies; }}Copy the code
public void test(a) {
        final OkHttpClient client = new OkHttpClient().newBuilder()
                .followRedirects(false)
                .followSslRedirects(false)
                .cookieJar(new LocalCookieJar())
                .build();
        // Construct a POST request
        RequestBody body = new FormBody.Builder().add("UserStyle"."student")
                .add("user"."xxx").add("password"."xxx").build();

        Request request = new Request.Builder().url("http://222.195.8.201/pass.asp").post(body).build();
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                /** * If you don't use the CookieJar, then you have to parse the returned set-cookie field yourself, and then add the Cookie request header */ by addHeader("Cookie", Cookie) */
// List
      
        cookies = response.headers("Set-Cookie");
      
// String cookie = "";
// for(int i=cookies.size()-1; i>=0; i--){
// cookie = cookie+ cookies.get(i).replace("path=/", "") + " ";
/ /}

                // make a GET request
                Request redirectRequest = new Request.Builder().url("http://222.195.8.201/student/asp/Select_Success.asp")
// .addHeader("Cookie", cookie)
                        .build();
                // Get the content of a web page you logged in to
                Response response2 = client.newCall(redirectRequest).execute();
                String result = response2.body().string();
                System.out.println(result);
            }

            @Override
            public void onFailure(Call arg0, IOException arg1) {}}); }Copy the code