Java uses NIO to construct http requests and javanio to construct requests

Source: Internet
Author: User

Java uses NIO to construct http requests and javanio to construct requests

Use java NIO to construct an http request body and obtain the response content.

Package com. test. nio; import java.net. inetSocketAddress; import java. nio. byteBuffer; import java. nio. channels. socketChannel; import java. nio. charset. charset; public class TestSocketForBaidu {/*** @ param args * @ throws Exception */private static Charset charset = Charset. forName ("UTF8"); // create the GBK character set public static void main (String [] args) throws Exception {SocketChannel channel = SocketChannel. open (new Inet SocketAddress ("www.itbuluoge.com", 80); String line = "GET/HTTP/1.1 \ r \ n"; line + = "HOST: www.itbuluoge.com \ r \ n "; line + = "\ r \ n"; channel. write (charset. encode (line); ByteBuffer buffer = ByteBuffer. allocate (1024); // create a buffer of 1024 bytes int size = channel. read (buffer); while (size! =-1) {buffer. flip (); while (buffer. hasRemaining () {System. out. print (charset. decode (buffer);} buffer. clear (); size = channel. read (buffer );}}}


Output result


We can see that the home page of the website can be returned and the body can be parsed. Note that when constructing an http request, the minimum required protocol or parameter is:

GET, HTTP, 1.1

Host: www.itbuluoge.com


Note that the end must be two "\ r \ n ".


How to Use the Java language to construct an http header to call any API function is best to illustrate how to connect to the WebQQ server to obtain the verification code,

Create an HTTP request header? Not very familiar with the meaning of the landlord. Do you want to implement communication between the browser and the server? The B/S mode is a request/response interaction mode. The request is the HTTP request header. The SCOKET mechanism in JAVA Network programming can be implemented. But for JAVA, its length is not in this aspect. After all, it is a high-level language and cannot be compared with lower-level advanced languages such as C/C ++ in terms of running speed.

I used java nio to copy files, but I didn't find any obvious advantage in operating with the traditional stream. Why?

Read the Thinking in java document. From 1.5 onwards, Java has rewritten InputStream/OutputStream and uses NIO. Therefore, even if you do not display the declaration, you must use NIO, as long as your class inherits InputStream/OutputStream, NIO is already in use. If you don't believe it, do this.
FileChannel channel = new FileInputStream. getChannel ();
If XXStream is not constructed using NIO, how does one return a Channel object?

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.