Java Socket Impersonation HTTP request

Source: Internet
Author: User

We simulate the Sslsocket connection, request the blog home page, and print the response message. Code reference from "Java Network programming Fine solution" Sun Weichen

Write this blog to illustrate some of the problems in the simulation process, please first access the target path in the browser, F12 view the network, view the original network request header.

Attention:

The 1.HTTP request message contains the request line, the request header, the blank line, the request package Body 4 parts.

2. accept-encoding do not use compression when simulating request header, otherwise the response result will appear garbled

3. Request end of header use carriage return end of line break

The code is as follows:

 Packagecom.beluga.hello.http;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.Socket; Public classtesthttpclient {Socket Socket=NULL;  Public voidCreatesocket () {Try{Socket=NewSocket ("www.javathinker.org", 80); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         Public voidcommuncate () {//Note that there must be a request method address note spaceStringBuffer SB =NewStringBuffer ("GET http://www.javathinker.org/http/1.1\r\n"); //The following is the request headerSb.append ("host:www.javathinker.org\r\n"); Sb.append ("user-agent:mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) gecko/20100101 firefox/53.0\r\n "); Sb.append ("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"); Sb.append ("accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); //Note here do not use compression or return garbledSb.append ("accept-encoding: \ r \ n"); Sb.append ("Connection:keep-alive\r\n"); Sb.append ("Upgrade-insecure-requests:1\r\n"); //Note that you want to wrap the end request header here.Sb.append ("\ r \ n");        System.out.println (Sb.tostring ()); Try{outputstream OS=Socket.getoutputstream ();            Os.write (Sb.tostring (). GetBytes ()); InputStream is=Socket.getinputstream (); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); byte[] bytes =New byte[1024]; intLen =-1;  while(len = is.read (bytes))! =-1) {baos.write (bytes,0, Len); } System.out.println (NewString (Baos.tobytearray ()));        Socket.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         Public Static voidMain (string[] args) {testhttpclient client=Newtesthttpclient ();        Client.createsocket ();    Client.communcate (); }}

Java Socket Impersonation HTTP request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.