Java Tutorial/Network/URLConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:03, 1 июня 2010
Содержание
- 1 All Headers
- 2 Call a servlet from a Java command line application
- 3 Chain the InputStream to a Reader
- 4 Downloading a web page using URL and URLConnection classes
- 5 Encoding Aware Source Viewer
- 6 Get files updated last 24 hours
- 7 Get response header from HTTP request
- 8 Getting the Cookies from an HTTP Connection
- 9 Getting the Response Headers from an HTTP Connection
- 10 Header Viewer
- 11 Identify yourself using HTTP Authentification
- 12 java.net.URLConnection
- 13 Preventing Automatic Redirects in a HTTP Connection
- 14 Read a GIF or CLASS from an URL and save it locally
- 15 Read from a URL with buffered stream
- 16 Sending a Cookie to an HTTP Server
- 17 Sending a POST Request Using a URL
- 18 Sending a POST Request with Parameters From a Java Class
- 19 URLConnection.openStream is more powerful than URL.openStream
- 20 Writing to a Web server
All Headers
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main(String args[]) throws Exception {
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
for (int j = 1;; j++) {
String header = uc.getHeaderField(j);
if (header == null)
break;
System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
}
}
}
Call a servlet from a Java command line application
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class CounterApp {
String servletURL;
String sessionCookie = null;
public static void main(String args[]) {
if (args.length == 0) {
System.out.println("\nServlet URL must be specified");
return;
}
try {
CounterApp app = new CounterApp(args[0]);
for (int i = 1; i <= 5; i++) {
int count = app.getCount();
System.out.println("Pass " + i + " count=" + count);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public CounterApp(String url) {
servletURL = url;
}
public int getCount() throws Exception {
java.net.URL url = new java.net.URL(servletURL);
java.net.URLConnection con = url.openConnection();
if (sessionCookie != null) {
con.setRequestProperty("cookie", sessionCookie);
}
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
out.flush();
byte buf[] = byteOut.toByteArray();
con.setRequestProperty("Content-type", "application/octet-stream");
con.setRequestProperty("Content-length", "" + buf.length);
DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
dataOut.write(buf);
dataOut.flush();
dataOut.close();
DataInputStream in = new DataInputStream(con.getInputStream());
int count = in.readInt();
in.close();
if (sessionCookie == null) {
String cookie = con.getHeaderField("set-cookie");
if (cookie != null) {
sessionCookie = parseCookie(cookie);
System.out.println("Setting session ID=" + sessionCookie);
}
}
return count;
}
public String parseCookie(String raw) {
String c = raw;
if (raw != null) {
int endIndex = raw.indexOf(";");
if (endIndex >= 0) {
c = raw.substring(0, endIndex);
}
}
return c;
}
}
Chain the InputStream to a Reader
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main(String[] args) throws Exception {
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
InputStream raw = uc.getInputStream();
InputStream buffer = new BufferedInputStream(raw);
// chain the InputStream to a Reader
Reader r = new InputStreamReader(buffer);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
}
}
Downloading a web page using URL and URLConnection classes
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws Exception {
URLConnection urlc = new URL("http://www.google.ru").openConnection();
BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
int byteRead;
while ((byteRead = buffer.read()) != -1){
System.out.println((char) byteRead);
}
buffer.close();
}
}
Encoding Aware Source Viewer
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main(String[] args) throws Exception {
String encoding = "ISO-8859-1";
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int encodingStart = contentType.indexOf("charset=");
if (encodingStart != -1) {
encoding = contentType.substring(encodingStart + 8);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in, encoding);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
}
}
Get files updated last 24 hours
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class MainClass {
public static void main(String[] args) throws Exception {
Date today = new Date();
long millisecondsPerDay = 24 * 60 * 60 * 1000;
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
uc.setIfModifiedSince((new Date(today.getTime() - millisecondsPerDay)).getTime());
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
}
}
Get response header from HTTP request
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.google.ru/index.html");
URLConnection connection = url.openConnection();
Map responseMap = connection.getHeaderFields();
for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
System.out.println(key + " = ");
List values = (List) responseMap.get(key);
for (int i = 0; i < values.size(); i++) {
Object o = values.get(i);
System.out.println(o + ", ");
}
}
}
}
Getting the Cookies from an HTTP Connection
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
for (int i = 0;; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName)) {
String[] fields = headerValue.split(";\\s*");
for (int j = 1; j < fields.length; j++) {
if ("secure".equalsIgnoreCase(fields[j])) {
System.out.println("secure=true");
} else if (fields[j].indexOf("=") > 0) {
String[] f = fields[j].split("=");
if ("expires".equalsIgnoreCase(f[0])) {
System.out.println("expires"+ f[1]);
} else if ("domain".equalsIgnoreCase(f[0])) {
System.out.println("domain"+ f[1]);
} else if ("path".equalsIgnoreCase(f[0])) {
System.out.println("path"+ f[1]);
}
}
}
}
}
}
}
Getting the Response Headers from an HTTP Connection
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
for (int i = 0;; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
System.out.println(headerName);
System.out.println(headerValue);
if (headerName == null && headerValue == null) {
System.out.println("No more headers");
break;
}
}
}
}
Header Viewer
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class MainClass {
public static void main(String args[]) throws Exception {
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
System.out.println("Content-type: " + uc.getContentType());
System.out.println("Content-encoding: " + uc.getContentEncoding());
System.out.println("Date: " + new Date(uc.getDate()));
System.out.println("Last modified: " + new Date(uc.getLastModified()));
System.out.println("Expiration date: " + new Date(uc.getExpiration()));
System.out.println("Content-length: " + uc.getContentLength());
}
}
Content-type: text/html Content-encoding: null Date: Thu May 24 18:41:00 PDT 2007 Last modified: Fri May 18 08:20:08 PDT 2007 Expiration date: Wed Dec 31 16:00:00 PST 1969 Content-length: 345648
Identify yourself using HTTP Authentification
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
URLConnection conn = new URL("http://www.yourserver.ru").openConnection();
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "asdfasdf");
conn.connect();
InputStream in = conn.getInputStream();
}
}
java.net.URLConnection
- A URLConnection object represents a connection to a remote machine.
- You use it to read a resource from and write to a remote machine.
- To obtain an instance of URLConnection, call the openConnection method on a URL object.
- To use a URLConnection object to write: set the value of doOutput to true using setDoOutput methods:
URL url = new URL ("http://www.google.ru/");
InputStream inputStream = url.openStream ();
Preventing Automatic Redirects in a HTTP Connection
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
HttpURLConnection.setFollowRedirects(false);
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setInstanceFollowRedirects(false);
conn.connect();
}
}
Read a GIF or CLASS from an URL and save it locally
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String args[]) throws Exception {
byte[] b = new byte[1];
URL url = new URL("http://www.server.ru/a.gif");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
DataInputStream di = new DataInputStream(urlConnection.getInputStream());
FileOutputStream fo = new FileOutputStream("a.gif");
while (-1 != di.read(b, 0, 1))
fo.write(b, 0, 1);
di.close();
fo.close();
}
}
Read from a URL with buffered stream
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main(String[] args) throws Exception {
URL u = new URL(args[0]);
URLConnection uc = u.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String s = br.readLine();
while (s != null) {
System.out.println(s);
s = br.readLine();
}
}
}
Sending a Cookie to an HTTP Server
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
conn.connect();
}
}
Sending a POST Request Using a URL
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Main {
public static void main(String[] argv) throws Exception {
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
URL url = new URL("http://server.ru:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
}
}
Sending a POST Request with Parameters From a Java Class
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.jexp.ru");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("value=1&anotherValue=1");
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
}
}
URLConnection.openStream is more powerful than URL.openStream
Reading a Web resource"s headers and content:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru/");
URLConnection urlConnection = url.openConnection();
Map<String, List<String>> headers = urlConnection.getHeaderFields();
Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
for (Map.Entry<String, List<String>> entry : entrySet) {
String headerName = entry.getKey();
System.out.println("Header Name:" + headerName);
List<String> headerValues = entry.getValue();
for (String value : headerValues) {
System.out.print("Header value:" + value);
}
System.out.println();
System.out.println();
}
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
while (line != null) {
System.out.println(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Header Name:Content-Length Header value:329495 Header Name:X-Powered-By Header value:ASP.NET Header Name:ETag Header value:"30d67746a17c71:11e5" Header Name:null Header value:HTTP/1.1 200 OK Header Name:Date Header value:Fri, 26 Jan 2007 17:02:42 GMT Header Name:Accept-Ranges Header value:bytes Header Name:Content-Type Header value:text/html Header Name:Server Header value:Microsoft-IIS/6.0 Header Name:Last-Modified Header value:Mon, 04 Dec 2006 06:04:11 GMT Header Name:Content-Location Header value:http://www.jexp.ru/index.htm
Writing to a Web server
You can use a URLConnection object to send an HTTP request.
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main(String[] a)throws Exception {
URL url = new URL("http://www.yourdomain.ru/form.jsp");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("firstName=Joe");
out.println("lastName=Average");
out.close();
}
}