esoe
2 years ago
5 changed files with 72 additions and 3 deletions
@ -0,0 +1,57 @@ |
|||||||
|
package ru.molokoin.sourceListener; |
||||||
|
import java.net.*; |
||||||
|
import java.io.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* This program demonstrates a client socket application that connects to |
||||||
|
* a web server and send a HTTP HEAD request. |
||||||
|
* |
||||||
|
* @author www.codejava.net |
||||||
|
*/ |
||||||
|
public class HttpClient { |
||||||
|
URL url; |
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
HttpClient cli = new HttpClient(); |
||||||
|
try { |
||||||
|
cli.url = new URL("http://www.molokoin.ru"); |
||||||
|
} catch (MalformedURLException ex) { |
||||||
|
ex.printStackTrace(); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
String hostname = cli.url.getHost(); |
||||||
|
int port = 3000; |
||||||
|
|
||||||
|
try (Socket socket = new Socket(hostname, port)) { |
||||||
|
|
||||||
|
OutputStream output = socket.getOutputStream(); |
||||||
|
PrintWriter writer = new PrintWriter(output, true); |
||||||
|
|
||||||
|
writer.println("HEAD " + cli.url.getPath() + " HTTP/1.1"); |
||||||
|
writer.println("Host: " + hostname); |
||||||
|
writer.println("User-Agent: Simple Http Client"); |
||||||
|
writer.println("Accept: text/html"); |
||||||
|
writer.println("Accept-Language: en-EN"); |
||||||
|
writer.println("Connection: close"); |
||||||
|
writer.println(); |
||||||
|
|
||||||
|
InputStream input = socket.getInputStream(); |
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); |
||||||
|
|
||||||
|
String line; |
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null) { |
||||||
|
System.out.println(line); |
||||||
|
} |
||||||
|
} catch (UnknownHostException ex) { |
||||||
|
|
||||||
|
System.out.println("Server not found: " + ex.getMessage()); |
||||||
|
|
||||||
|
} catch (IOException ex) { |
||||||
|
|
||||||
|
System.out.println("I/O error: " + ex.getMessage()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,8 +1,20 @@ |
|||||||
package ru.molokoin.sourceListener; |
package ru.molokoin.sourceListener; |
||||||
|
|
||||||
public class SourceListener { |
public class SourceListener { |
||||||
|
|
||||||
public static void main(String[] args) { |
public static void main(String[] args) { |
||||||
System.out.println(Thread.currentThread().getName()); |
HttpClient client = HttpClient.newBuilder() |
||||||
|
.version(Version.HTTP_1_1) |
||||||
|
.followRedirects(Redirect.NORMAL) |
||||||
|
.connectTimeout(Duration.ofSeconds(20)) |
||||||
|
.proxy(ProxySelector.of(new InetSocketAddress("www.ya.ru", 80))) |
||||||
|
.authenticator(Authenticator.getDefault()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send("GET", BodyHandlers.ofString()); |
||||||
|
System.out.println(response.statusCode()); |
||||||
|
System.out.println(response.body()); |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue