本文将为小白讲解Java中的网络通信:HTTP客户端、HTTP服务器、Web服务。通过本文,您将学会Java中的网络通信基础知识,包括如何使用HTTP客户端、HTTP服务器和Web服务。
HTTP客户端是一种用于发送HTTP请求的程序。在Java中,您可以使用HttpURLConnection类或Apache HttpClient库来创建HTTP客户端。
HttpURLConnection类是Java中的标准HTTP客户端。下面是一个使用HttpURLConnection类发送HTTP GET请求的示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println(content.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Apache HttpClient库是一个流行的Java HTTP客户端库。下面是一个使用Apache HttpClient库发送HTTP GET请求的示例:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class HttpClientExample {
public static void main(String[] args) {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://www.example.com");
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println(content.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
HTTP服务器是一种用于接收HTTP请求并返回HTTP响应的程序。在Java中,您可以使用Java HTTP服务器API或Spring Boot框架来创建HTTP服务器。
Java HTTP服务器API是Java SE 6中的新功能。下面是一个使用Java HTTP服务器API创建HTTP服务器的示例:
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
public class HttpServerExample {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new TestHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class TestHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Spring Boot是一个流行的Java框架,它提供了创建HTTP服务器的简便方法。下面是一个使用Spring Boot创建HTTP服务器的示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HttpServerExample {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HttpServerExample.class, args);
}
}
Web服务是一种通过Web进行交互的软件系统。在Java中,您可以使用Java API for RESTful Web Services(JAX-RS)或Spring Boot框架来创建Web服务。
JAX-RS是Java的RESTful Web服务API。下面是一个使用JAX-RS创建Web服务的示例:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello, world!";
}
}
Spring Boot提供了创建RESTful Web服务的简便方法。下面是一个使用Spring Boot创建Web服务的示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, world!";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com