Java Socket httpclient get

Source: Internet
Author: User

 

Public   Class Httpclientmain {

/**
*
@ Param ARGs
*/
Public   Static   Void Main (string [] ARGs ){
// Todo auto-generated method stub
Socket socket;
Try {
Inetaddress hostadd
= Inetaddress. getbyname ( " 192.168.6.5 " );
Socket
= New Socket (hostadd, 80 );
Outputstream out
= Socket. getoutputstream ();
Inputstream in
= Socket. getinputstream ();

Bytearrayoutputstream baos = New Bytearrayoutputstream ();
// Baos. Write ("Data = ABC \ r \ n". getbytes ());
// System. Out. println ("Data lenth:" + baos. tobytearray (). Length );

Byte [] Bytes = ( " GET/HTTP/1.1 \ n "   +
" HOST: 192.168.6.5 \ n "   +
" Connection: Close \ n "   +
" User-Agent: Mozilla/4.0 \ n "   +
" Accept-language: ZH-CN \ n "   +
" \ N " ). Getbytes ( " UTF-8 " );

Out. Write (bytes );
Out. Flush ();
Byte [] BT = New   Byte [ 1024 ];
Inputstreamreader ISR;
Int Len;
Baos. Reset ();
While (Len = In. Read (BT )) >- 1 ){
Baos. Write (BT,
0 , Len );
}
Vector
< String > V = Getstartendvalue (baos. tostring (), " \\( " , " \\) " , 0 );
If (V. Size () > 0 ){
System. Out. println (V. firstelement ());
}
Baos. Close ();
In. Close ();
}
Catch (Unknownhostexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Catch (Ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

// /<Summary>
// /Obtain the value between the start and end strings.
// /</Summary>
// /<Param name = "str"> string </param>
// /<Param name = "S"> Start </param>
// /<Param name = "E"> end </param>
// /<Param name = "azimuth"> whether the matched content contains the start and end symbols. 0: Not including 1: Include </param>
// /<Returns> </returns>

Public   Static Vector < String > Getstartendvalue (string STR, string S, string e, Int Azimuth ){
Pattern pattern;
If (Azimuth = 1 ){
Pattern
= Pattern. Compile ( " (? = ( "   + S +   " ) [. \ S] *? (? <= ( "   + E +   " )) " );
}
Else {
Pattern
= Pattern. Compile ( " (? <= ( "   + S +   " ) [. \ S] *? (? = ( "   + E +   " )) " );
}
Matcher
= Pattern. matcher (STR );
Stringbuffer Buffer
=   New Stringbuffer ();
Vector
< String > Vector = New Vector < String > ();
While (Matcher. Find ()){
Vector. Add (matcher. Group ());
}
Return Vector;
}
}

 

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.