Both methods http request with analog transmission of cookies

If you want to send a request with cookies, there are two ways, using a tool, using a java code, dry follows:

Tools
tools used postman and Postman Interceptor Extended download Google browser (requires scientific access or modify the native host, but this method is a little trouble spots)
using a request postman sent with cookies, you must start the Google browser and the postman both the Interceptor, indispensable


First, it must have JavaScript enabled to view postman and the Interceptor, and then, like normal hair postman's request, postman will directly read Google browser cookies and own and get / post requests sent out together

java code
using the code sent, in fact, very short answer. Here are the easiest way to
send cookies header using the header parameter

doPostCookie static String public (String URL) {
        the try {
            HttpPost new new HttpPost Request = (URL);
            request.addHeader ( "cookies", "the JSESSIONID = 4A3998E6FCA477D878BFF99C26FB1608");
            return Execute (Request);
        } the catch (Exception E) {
            the throw new new a RuntimeException (String.format ( "HTTP POST Fail [Message =% S]", e.getMessage ()));
        }
    }
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
wherein request.addHeader is to add a cookies. This displays cookies in the browser as follows:

(See cookies plug-in is a Google browser plug-in here called editthiscookie)
excute method code above is as follows:

private static String execute( HttpUriRequest request ) {
        try {
            HttpResponse response = HTTP_CLIENT.execute( request );
            StatusLine statusLine = response.getStatusLine();
            if( null == statusLine ) {
                throw new RuntimeException( "http request fail, no status line");
            }
            if( statusLine.getStatusCode() != HttpStatus.SC_OK ) {
                throw new RuntimeException(String.format( "http request fail[status=%d|message=%s]", statusLine.getStatusCode(),
                        EntityUtils.toString( response.getEntity(), CONTENT_CHARSET )));
            }

            return EntityUtils.toString( response.getEntity(), CONTENT_CHARSET );
        } catch( RuntimeException ex ) {
            LOGGER.error(String.format("execute http request fail[url=%s|msg=%s]", request.getURI(), ex.getMessage()));
            throw ex;
        } catch( Exception ex ) {
            LOGGER.error( String.format( "http request fail[msg=%s|url=%s|param=%s]", ex.getMessage(), request.getURI(),
                    JSON.toJSONString( request.getParams() ) ) );
            throw new RuntimeException("http request fail");
        } finally {
            if( null != request && !request.isAborted() ) {
                request.abort();
            }
        }
    }
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
to be imported packages:

        <!--http -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>3.7</version>
        </dependency>
        <!--http -->
1
2
3
4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
over. . . . . . . . (You can add qq475804848 communication technologies oh ????)

PS:
Also, if you want to help you set up a browser cookies, you can use the following method:
// Set the cookie

response.addHeader(“Set-Cookie”, “uid=112; Path=/; HttpOnly”);

// set multiple cookie

response.addHeader(“Set-Cookie”, “uid=112; Path=/; HttpOnly”);

response.addHeader(“Set-Cookie”, “timeout=30; Path=/test; HttpOnly”);

// set the cookie https

response.addHeader(“Set-Cookie”, “uid=112; Path=/; Secure; HttpOnly”);
 

Published 91 original articles · won praise 47 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_30007885/article/details/103730824