1 Importjava.io.IOException;2 ImportJava.io.InputStream;3 ImportJava.io.OutputStreamWriter;4 Importjava.net.InetAddress;5 ImportJava.net.Socket;6 Importjava.net.UnknownHostException;7 Importjava.util.ArrayList;8 9 /**Ten * A simple HTTP client that sends an HTTP request that simulates the HTTP message sent over by the browser printable server One */ A Public classHttpClient { - Private StaticString encoding = "UTF-8"; - the Public Static voidMain (string[] args) { - Try { -Socket s =NewSocket (Inetaddress.getlocalhost (), 8080); -OutputStreamWriter OSW =NewOutputStreamWriter (S.getoutputstream ()); +StringBuffer SB =NewStringBuffer (); -Sb.append ("get/slsint_gd/reportview/reportviewlist.jsp http/1.1\r\n"); +Sb.append ("host:localhost:8088\r\n"); ASb.append ("connection:keep-alive\r\n"); at //Note that this is the key to the key and forget to let me engage in this for half an hour. Here must be a carriage return line, indicating the end of the message, or the server will wait -Sb.append ("\ r \ n"); - Osw.write (sb.tostring ()); - Osw.flush (); - - //--header information of the message returned by the output server inInputStream is =S.getinputstream (); -String line =NULL; to intcontentlength = 0;//message length sent back by the server + //read all requests sent over the server header information - Do { theLine = ReadLine (IS, 0); * //If a content-length message header is removed $ if(Line.startswith ("Content-length")) {Panax NotoginsengContentLength = Integer.parseint (Line.split (":") [1].trim ()); - } the //Print Request Department information + System.out.print (line); A //If a separate carriage return line is encountered, the request header ends the} while(!line.equals ("\ r \ n")); + - //--the body of the loss message $ System.out.print (ReadLine (IS, contentlength)); $ - //Close the stream - is.close (); the -}Catch(unknownhostexception e) {Wuyi e.printstacktrace (); the}Catch(IOException e) { - e.printstacktrace (); Wu } - } About $ /* - * Here we ourselves simulate reading a line, because if you use BufferedReader in the API, it is read to a carriage return after a newline - * Only return, otherwise if not read, it is blocked, the direct server time-out automatically shut down, if you also use BufferedReader at this time - * To read, because the last line is read, there will be no carriage return newline character, so will wait. If you use the server to send back A * Content-length in the message header to intercept the message body so that it does not block + * the * The Contentle parameter, if it is 0 o'clock, indicates a read header, we still return one line at a time, if not 0, the body of the message is read, - * When we read the body of the message based on the length of the message body, the client automatically closes the stream so that it does not have to be shut down before the server times out. $ */ the Private StaticString ReadLine (InputStream is,intContentle)throwsIOException { theArrayList linebytelist =NewArrayList (); the byteReadByte; the intTotal = 0; - if(Contentle! = 0) { in Do { theReadByte = (byte) Is.read (); the Linebytelist.add (byte.valueof (ReadByte)); Abouttotal++; the} while(Total < Contentle);//message body read not yet read out the}Else { the Do { +ReadByte = (byte) Is.read (); - Linebytelist.add (byte.valueof (ReadByte)); the} while(ReadByte! = 10);Bayi } the the byte[] Tmpbytearr =New byte[Linebytelist.size ()]; - for(inti = 0; I < linebytelist.size (); i++) { -Tmpbytearr[i] =((Byte) linebytelist.get (i)). Bytevalue (); the } the linebytelist.clear (); the the return NewString (Tmpbytearr, encoding); - } the}
http/1.1 OK
server:apache-coyote/1.1
set-cookie:jsessionid=1f27447b2cdf30a69009a219932e259d; path=/slsint_gd/; HttpOnly
Content-type:text/html;charset=utf-8
Content-language:zh-cn
content-length:1881
Date:mon, 09:11:44 GMT
Socket Impersonation HTTP client request