Catalog

One 、 brief introduction :HTTP Programming

1、HTTP The system design

2、HTTP Client working process

3、HTTP The working process of the server

Two 、 be based on TCP Socket Of HTTP Website download

3、 ... and 、 be based on SSL Socket Of HTTPS Website download

Four 、HTTP Client complete code

5、 ... and 、 Interface integrity code

6、 ... and 、 Last + demonstration


One 、 brief introduction :HTTP Programming

Final review HTTP Network programming , Main learning records HTTP(s) Network programming of protocol , Including the use of TCP Socket Three handshakes HTTP Website download , And use SSL Socket Safe transmission of HTTPs Website download , Complete the programming by case practice , know http(s) The actual working mechanism of !

current HTTP The client is much more complex than the earlier ones , It's not just about downloading and displaying web files , There are many new features : Cross platform display 、 Parameter passing 、 The realization of dynamic web page and user interaction, etc .

1、HTTP The system design

  • Client software (web browser :Chrome、360 The browser etc. )
  • Server software (web The server : Microsoft IIS、Apache Tomcat)

2、HTTP Client working process

  • The client software establishes a connection with the server (TCP Three handshakes of );
  • send out HTTP Header format protocol ;
  • Receiving web files ;
  • Show web page .

3、HTTP The working process of the server

  • Turn on the server software 80 port ;
  • Respond to customer requests 、 complete TCP Connect ;
  • Check the client's HTTP Header format to send the web file requested by the customer ( With dynamic web pages ).

chart 1 HTTP request - Respond to the complete process

Web download technology is a search engine 、 Web crawler 、 Web page collector or network push services and other related applications in the field of basic technology , Here are two protocols used in daily life (http and https) Web access to download .

Two 、 be based on TCP Socket Of HTTP Website download

about TCP Socket connection process has a very deep understanding of , It has also been used to test communication locally TCP Of Socket Establishing a connection , Empathy , And HTTP Server establishes connection , It's also the use of TCP Information exchange .

Once the connection is established , Need to send HTTP Request header , The server confirms the requester , Turn on communication at both ends , The client can receive web file information , After rendering, the web page will be displayed . Here we realize to receive web page file information first , In the next article, we will realize the function of the browser after rendering the web page .

With www.baidu.com For example , And HTTP After the server establishes the connection , We need to send a web request , That is to say HTTP Request header . Construct the request header as follows :

GET / HTTP/1.1

HOST: www.baidu.com

Accept: */*

Accept-Language: zh-cn

User-Agent: User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64)

Connection: Keep-Alive

It needs to be sent strictly in accordance with the format , And commonly used StringBuffer Class toString() Method can complete HTTP The request header is converted to a string , Send consistently to HTTP The server .

StringBuffer msg = new StringBuffer();
msg.append("GET / HTTP/1.1\r\n"+
"HOST: "+domainName+"\r\n"+
"Accept: */*\r\n"+
"Accept-Language: zh-CN\r\n"+
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\n"+
"Connection: Keep-Alive\r\n"
);

Line breaks use \r\n To avoid errors due to coding problems .

After sending the request, if the first message returned by the web information display area is “HTTP/1.1 200 OK”, It means that the visit is normal .

You can see HTTP The server returns a lot of information , This is also the response header , Contains a lot of key information content .

3、 ... and 、 be based on SSL Socket Of HTTPS Website download

Based on the above design TCP Communication transmission HTTP, We try to visit www.sina.com.cn, It turns out that the first line of the response header is HTTP/1.1 302 Moved Temporarily( Site removed ), For safety reasons , Now most of the web The site will give up HTTP Enabling HTTPS, They're all using secure encryption HTTPS agreement , And shut it down HTTP, Only enabled SSL/TLS Of HTTPS A secure connection , By default, this connection uses 443 port . therefore TCP Socket There is no normal way to visit the web page .

It's just that the port was changed to 443 Can it be normal , The answer is as follows .

The reason can also be seen in the front , Need to use SSL/TLS Of HTTPS A secure connection , To establish and HTTPS Server communication , So it needs to be modified Socket type .

It's used here Java Secure socket extension (Java Secure Socket Extension,JSSE), be based on SSL and TLS Agreed Java Web applications provide Java API And reference implementation , The client's SSLSocket Socket .SSLSocket Relative to the client socket I learned before , It's just that the creation method is different ,SSLSocket Object by SSLSocketFactory establish .

Declare member variables in a class and create Socket Connect :

private SSLSocket socket;
private SSLSocketFactory factory; factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
socket=(SSLSocket)factory.createSocket(ip,Integer.parseInt(port));

Yes SSL Socket Use and TCP identical , It's just that the creation method is different , After a little modification , Can successfully request HTTPS Web page information of the website .

Four 、HTTP Client complete code

Here is given HTTP The complete code of the client ,HTTPS Just change the above SSL Socket.

/*
* HTTPClient.java
* Copyright (c) 2020-12-21
* author : Charzous
* All right reserved.
*/ package chapter08; import java.io.*;
import java.net.Socket; public class HTTPClient {
private Socket socket; private PrintWriter pw;
private BufferedReader br;
/**
* @param ip
* @param port
* @return
* @author Charzous
* @date 2020/12/21 14:52
*
*/
public HTTPClient(String ip, String port) throws IOException{
// Actively initiate a connection to the server , Realization TCP Three handshakes
// If you don't succeed, you throw an error , It's up to the caller to handle the error
socket =new Socket(ip,Integer.parseInt(port)); // Get the network stream output byte stream address , And encapsulate it into network output character stream
OutputStream socketOut=socket.getOutputStream();
// Parameters true Indicates automatic flush data
pw=new PrintWriter(new OutputStreamWriter(socketOut,"utf-8"),true); // Get the network input byte stream address , And encapsulated into a network input character stream
InputStream socketIn=socket.getInputStream();
br=new BufferedReader(new InputStreamReader(socketIn,"utf-8")); } public void send(String msg) throws InterruptedException {
// Output character stream , from socket Call the underlying system function , Send byte stream through network card
try {
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
} pw.println(msg);
} public String receive(){
String msg=null;
try {
// Read information from a stream of network input characters , Only one line of information can be accepted at a time
// When you don't have a line ( No line terminator ), The statement blocks
// Until the conditions are met , The program runs down
msg=br.readLine();
}catch (IOException e){
e.printStackTrace();
}
return msg;
} public void close(){
try {
if (socket!=null)
socket.close();
}catch (IOException e){
e.printStackTrace();
}
} }

5、 ... and 、 Interface integrity code

I use a graphical interface to access http and https, Integrate the functions of the above two graphics clients , Make the graphic client can access 443 Of https Content , You can also visit non 443 port ( It's usually 80) Of http Content .

/*
* HTTPAllClientFX.java
* Copyright (c) 2020-12-21
* author : Charzous
* All right reserved.
*/ package chapter08; import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage; public class HTTPAllClientFX extends Application { private Button btnExit=new Button(" sign out ");
private Button btnSend = new Button(" Web request "); // private TextField tfSend=new TextField();// Input information area private TextArea taDisplay=new TextArea();// Display area
private TextField ipAddress=new TextField();// Fill in ip Address
private TextField tfport=new TextField();// Fill in the port
private Button btConn=new Button(" Connect ");
private HTTPSClient httpsClient;
private HTTPClient httpClient;
private Thread readThread; public static void main(String[] args) {
launch(args);
} @Override
public void start(Stage primaryStage) {
BorderPane mainPane=new BorderPane(); // Connect to the server zone
HBox hBox1=new HBox();
hBox1.setSpacing(10);
hBox1.setPadding(new Insets(10,20,10,20));
hBox1.setAlignment(Pos.CENTER);
hBox1.getChildren().addAll(new Label(" Web address :"),ipAddress,new Label(" port :"),tfport,btConn);
mainPane.setTop(hBox1); VBox vBox=new VBox();
vBox.setSpacing(10); vBox.setPadding(new Insets(10,20,10,20));
vBox.getChildren().addAll(new Label(" Web information display area "),taDisplay); VBox.setVgrow(taDisplay, Priority.ALWAYS);
mainPane.setCenter(vBox); HBox hBox=new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(10,20,10,20));
hBox.setAlignment(Pos.CENTER_RIGHT);
hBox.getChildren().addAll(btnSend,btnExit);
mainPane.setBottom(hBox); Scene scene =new Scene(mainPane,700,500);
primaryStage.setScene(scene);
primaryStage.show(); // Connect button
btConn.setOnAction(event -> {
String ip=ipAddress.getText().trim();
String port=tfport.getText().trim();
taDisplay.clear(); try {
if (port.equals("443")){
httpsClient = new HTTPSClient(ip, port);
// Successfully connected to the server , Accept the first welcome message from the server
taDisplay.appendText(" Server connection successful .\n"); readThread = new Thread(()->{
String receiveMsg=null;// Receive a string of characters from the server
if (port.equals("443")){
while ((receiveMsg=httpsClient.receive())!=null){
//lambda You cannot directly access external expressions final Type local variables , You need to define a temporary variable
// If the receiveMsg Defined as a class member variable , You don't need a temporary variable
String msgTemp = receiveMsg;
Platform.runLater(()->{
taDisplay.appendText(msgTemp+"\n");
});
}
}
});
readThread.start();
} else if (port.equals("80")){
httpClient = new HTTPClient(ip, port);
taDisplay.appendText(" Server connection successful .\n");
readThread = new Thread(()-> {
String receiveMsg = null;
while ((receiveMsg = httpClient.receive()) != null) {
String msgTemp = receiveMsg;
Platform.runLater(() -> {
taDisplay.appendText(msgTemp + "\n");
});
}
});
readThread.start();
} }catch (Exception e){
taDisplay.appendText(" Server connection failed !"+e.getMessage()+"\n");
}
}); // Web request button event
btnSend.setOnAction(event -> {
String ip=ipAddress.getText().trim();
String port=tfport.getText().trim();
String domainName=ipAddress.getText().trim();
try {
StringBuffer msg = new StringBuffer();
msg.append("GET / HTTP/1.1\r\n"+
"HOST: "+domainName+"\r\n"+
"Accept: */*\r\n"+
"Accept-Language: zh-CN\r\n"+
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\n"+
"Connection: Keep-Alive\r\n"
);
if (port.equals("443"))
httpsClient.send(msg.toString());
else if (port.equals("80"))
httpClient.send(msg.toString()); } catch (InterruptedException e) {
e.printStackTrace();
} }); btnExit.setOnAction(event -> {
try {
exit();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// Event in response to form closing , Click on the × close , The client is also shut down
primaryStage.setOnCloseRequest(event -> {
try {
exit();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
} private void exit() throws InterruptedException {
if (httpsClient!=null||httpClient!=null){
readThread.sleep(1000);// Multithread waiting , There are threads waiting to close the window IO, Set up 1s The interval ensures that all threads are shut down
httpsClient.close();
httpClient.close();
}
System.exit(0);
} }

6、 ... and 、 Last + demonstration

HTTP Connect www.baidu.com, success

HTTP Connect www.sina.com.cn, Failure

HTTPS Connect www.sina.com.cn, success

Final review , By the way, write a blog , This is the first one , Introduce HTTP Web request download , Mainly HTTP(s) Network programming of protocol , Including the use of TCP Socket Three handshakes HTTP Website download , And use SSL Socket Safe transmission of HTTPs Website download , Complete the programming by case practice , know http(s) The actual working mechanism of !

expect :Java And HTTP Network programming ( The next part : Web browser programming ), Will see the page of HTML Source code , And the web page rendered by browser function !

If you think it's good, welcome to “ One key, three links ” Oh , Like, collect, pay attention to , Comment directly if you have questions , Communication and learning !


My blog Park :https://www.cnblogs.com/chenzhenhong/p/14435762.html

my CSDN Blog :https://blog.csdn.net/Charzous/article/details/111470556


Copyright notice : This article is an original blog article , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .

Java And HTTP Network programming ( One ):TCP/SSL More related articles downloaded from the website

  1. Java Network programming -- be based on TCP Simulate multi-user login

    Java TCP Refer to the previous article for the basic operation of :Java Network programming – be based on TCP File upload Before implementing multi-user operations, implement the following single user operations , Suppose there's a user right now : account number :zs password :123 Server side : public c ...

  2. Java Study : Network programming summary

    Java Network programming summary One . summary Computer network is through transmission medium . Communication facilities and network communication protocols , Connecting computer devices scattered in different places , A system for resource sharing and data transmission . Network programming is to write programs to make the network of two ( Or more ) equipment ( ...

  3. Java Learning network programming examples

    from :http://www.cnblogs.com/springcsc/archive/2009/12/03/1616413.html  Thank you for sharing Network programming Network programming for many beginners , It's a dream ...

  4. Black horse programmer :Java Basic summary ---- Network programming

    Black horse programmer :Java Basic summary Network programming   ASP.Net+Android+IO Development . .Net train . Looking forward to communicating with you ! Network programming Elements of network communication . IP Address . Identification of devices in the network . It's hard to remember , You can use ...

  5. Java Advanced network programming

    Network programming Network programming for many beginners , It's a programming skill I'm looking forward to , But many beginners give up learning this part of technology because they can't enter the door of network programming for a long time . stay Before learning network programming , Many beginners may think that network editing ...

  6. The first 84 section :Java Network programming in ( in )

    The first 84 section :Java Network programming in ( in ) Communication between client and server : Actions required by the client , establish socket, Clear address and port , Keyboard entry , Get the data you need , Then send the entered data to the server , by socket Output stream , ...

  7. The first 78 section :Java Network programming in ( On )

    The first 78 section :Java Network programming in ( On ) Preface Network programming involves ip, port , agreement ,tcp and udp Understanding , And for socket Network details of communication . Network programming OSI Open Systems Interconnection Network programming refers to IO Adding network TCP/IP Model : ...

  8. The first 62 section : Explore Java Network programming technology in

    Preface thank ! Thanks to take care of ~ Explore Java Network programming technology in Network programming is io The combination of technology and network technology , The definition of network model , As long as you share the network model, you can connect the two . Network model reference . A tower has seven stories , We need to break through . The first physical layer ...

  9. java Section 9 Basic knowledge of network programming

    /** * * Basic knowledge of network programming * Network protocol and TCP/IP * IP Address and Port( Port number ) * Local loop's IP Address :127.0.0.1 * The range of port numbers is 0-65535 Between ,0-1023 The end between ...

  10. 20165324 Java Experiment five Network programming and security

    20165324 Java Experiment five Network programming and security One . The cover of the experiment report Course :Java Programming class :1653 class full name : He Chunjiang Student number :20165324 The instructor : Lou Jiapeng Date of experiment :2018 year 5 month 28 Japan real ...

Random recommendation

  1. Java Session Introduce ;

    Why Session This is to fill in Http The limitations of the agreement , When a user visits a page , The server returned the request ( Such as , You go through a web page , This page will display the content of the page , Interface UI For you ), Even if it's over , It's off , The server is no longer ...

  2. ytu 1939: Statistical vowels ( Water problem )

    Statistical vowels Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 68  Solved: 33[Submit][Status][Web Board] Descrip ...

  3. Actually ,SSL I don't care about everything when I get through ~~

    Too many of these man in the middle attacks need to be reinforced ~~ tested A It's a must !! https://www.ssllabs.com/ssltest/ It was two years ago , I wrote about it

  4. Hadoop Pseudo Distributed installation configuration

    Installation environment : System :Ubuntu 14.10   64bit hadoop:2.5.1 jdk:1.8.0_11 Installation steps : One . install JDK install jdk, And configure the environment and set it to the default sudo gedi ...

  5. How to configure FTP The server , Easy to operate server files

    1 Get into " Control panel "->" Program "->" Turn on or off Windows function ", find "Internet information service " Options ...

  6. Front-end UI Data display of design and interaction

    Appropriate data presentation can help users locate and browse data quickly , And working together more efficiently . The following points should be paid attention to in the design : According to the importance level of information . The frequency of operation and the degree of correlation are used to arrange the order of presentation . Pay attention to guidance in extreme situations . If the data information is too long , Content ...

  7. elasticsearch elk The most complete java api Search for polymerization 、 nested queries

    Catalog One . General inquiry ... 2 ( One ) matchAllQuery(client). 2 ( Two ) matchQuery(client);3 ( 3、 ... and ) multiMatchQuery(client);3 ...

  8. MOOC_Java Advanced _ Weng Kai said _ The third week question

    package mooc_java Advanced _d3 Weekly topic ; /** * Not used HashMap */ import java.util.ArrayList; import java.util.Scanner; ...

  9. adopt JS Dynamically create and delete HTML Elements

    <script type="text/javascript" language="Javascript"> function InputOnBlur ...

  10. jquery: Add click events to dynamically loaded elements

    jquery: Add click events to dynamically loaded elements Recently, I encountered such a problem when I was working on a project , For use ajax Add click events to dynamically loaded content , But it couldn't be triggered , After the inquiry test, the correct writing method is summed up stay jquery1.7 Before ...