Java by API/java.net/URLConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 20:43, 31 мая 2010
Содержание
- 1 URLConnection: connect() throws IOException
- 2 URLConnection: getContentEncoding()
- 3 URLConnection: getContentType()
- 4 URLConnection: getExpiration()
- 5 URLConnection: getHeaderField(int n)
- 6 URLConnection: getHeaderFieldKey(int n)
- 7 URLConnection: getHeaderFields()
- 8 URLConnection: getIfModifiedSince()
- 9 URLConnection: getInputStream()
- 10 URLConnection: getLastModified()
- 11 URLConnection: getURL()
- 12 URLConnection: setAllowUserInteraction(boolean allowuserinteraction)
- 13 URLConnection: setDoInput(boolean doinput)
- 14 URLConnection: setDoOutput(boolean dooutput)
- 15 URLConnection: setIfModifiedSince(long ifmodifiedsince)
- 16 URLConnection: setRequestProperty(String key, String value)
- 17 URLConnection: setUseCaches(boolean usecaches)
URLConnection: connect() throws IOException
<source lang="java">
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(); }
}
</source>
URLConnection: getContentEncoding()
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) { try { URL u = new URL("http://www.jexp.ru"); URLConnection uc = u.openConnection(); System.out.println("Content-encoding: " + uc.getContentEncoding()); } catch (MalformedURLException e) { System.err.println("not a URL I understand"); } catch (IOException e) { System.err.println(e); } }
}
</source>
URLConnection: getContentType()
<source lang="java">
import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; public class Main {
public static void main(String args[]) throws Exception { int c; URL hp = new URL("http://www.internic.net"); URLConnection hpCon = hp.openConnection(); long d = hpCon.getDate(); if (d == 0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); System.out.println("Content-Type: " + hpCon.getContentType()); d = hpCon.getExpiration(); if (d == 0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); d = hpCon.getLastModified(); if (d == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); int len = hpCon.getContentLength(); if (len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if (len != 0) { InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1)) { // && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No content available."); } }
}
</source>
URLConnection: getExpiration()
<source lang="java">
import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; public class Main {
public static void main(String args[]) throws Exception { int c; URL hp = new URL("http://www.internic.net"); URLConnection hpCon = hp.openConnection(); long d = hpCon.getDate(); if (d == 0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); System.out.println("Content-Type: " + hpCon.getContentType()); d = hpCon.getExpiration(); if (d == 0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); d = hpCon.getLastModified(); if (d == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); int len = hpCon.getContentLength(); if (len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if (len != 0) { InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1)) { // && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No content available."); } }
}
</source>
URLConnection: getHeaderField(int n)
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) { URL u; URLConnection uc; String header; try { u = new URL("http://www.jexp.ru"); uc = u.openConnection(); for (int j = 1;; j++) { header = uc.getHeaderField(j); if (header == null) break; System.out.println(uc.getHeaderFieldKey(j) + " " + header); } } catch (MalformedURLException e) { System.err.println("not a URL I understand."); } catch (IOException e) { System.err.println(e); } }
}
</source>
URLConnection: getHeaderFieldKey(int n)
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) { URL u; URLConnection uc; String header; try { u = new URL("http://www.jexp.ru"); uc = u.openConnection(); for (int j = 1;; j++) { header = uc.getHeaderField(j); if (header == null) break; System.out.println(uc.getHeaderFieldKey(j) + " " + header); } } catch (MalformedURLException e) { System.err.println("not a URL I understand."); } catch (IOException e) { System.err.println(e); } }
}
</source>
URLConnection: getHeaderFields()
<source lang="java"> import java.net.HttpURLConnection;
import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class Main {
public static void main(String[] args) throws Exception { String s; s = "http://www.y.ru/authTest"; URL url = new URL(s); URLConnection urlc = url.openConnection(); Map<String, List<String>> hf = urlc.getHeaderFields(); for (String key : hf.keySet()) System.out.println(key + ": " + urlc.getHeaderField(key)); System.out.println(((HttpURLConnection) urlc).getResponseCode()); }
}
</source>
URLConnection: getIfModifiedSince()
<source lang="java">
import java.net.URL; import java.net.URLConnection; import java.util.Date; public class MainClass {
public static void main(String[] args) { try { URLConnection uc = new URL("http://www.demo2s.ru").openConnection(); System.out.println("Will retrieve file if it"s been modified since " + new Date(uc.getIfModifiedSince())); uc.setIfModifiedSince(System.currentTimeMillis()); System.out.println("Will retrieve file if it"s been modified since " + new Date(uc.getIfModifiedSince())); } catch (Exception e) { System.err.println(e); } }
}
</source>
URLConnection: getInputStream()
<source lang="java">
import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; public class Main {
public static void main(String args[]) throws Exception { int c; URL hp = new URL("http://www.internic.net"); URLConnection hpCon = hp.openConnection(); long d = hpCon.getDate(); if (d == 0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); System.out.println("Content-Type: " + hpCon.getContentType()); d = hpCon.getExpiration(); if (d == 0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); d = hpCon.getLastModified(); if (d == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); int len = hpCon.getContentLength(); if (len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if (len != 0) { InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1)) { // && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No content available."); } }
}
</source>
URLConnection: getLastModified()
<source lang="java">
import java.net.URL; import java.net.URLConnection; public class Main {
public static void main(String[] argv) throws Exception { URL u = new URL("http://127.0.0.1/test.gif"); URLConnection uc = u.openConnection(); uc.setUseCaches(false); long timestamp = uc.getLastModified(); }
}
</source>
URLConnection: getURL()
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) { URL u; URLConnection uc; try { u = new URL("http://www.jexp.ru/"); try { uc = u.openConnection(); System.out.println(uc.getURL()); } catch (IOException e) { System.err.println(e); } } catch (MalformedURLException e) { System.err.println(e); } }
}
</source>
URLConnection: setAllowUserInteraction(boolean allowuserinteraction)
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String[] args) { try { MyHttpHandler handler = new MyHttpHandler(); URLConnection uc = handler.openConnection(new URL("http://www.ora.ru")); if (!uc.getAllowUserInteraction()) { uc.setAllowUserInteraction(true); } } catch (MalformedURLException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }
} class MyHttpHandler extends sun.net.www.protocol.http.Handler {
public URLConnection openConnection(URL u) throws IOException { return super.openConnection(u); }
}
</source>
URLConnection: setDoInput(boolean doinput)
<source lang="java">
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) throws Exception { String query = "name=yourname&email=youremail@yourserver.ru"; URLConnection uc = new URL("http:// your form ").openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setAllowUserInteraction(false); DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); // The POST line, the Accept line, and // the content-type headers are sent by the URLConnection. // We just need to send the data dos.writeBytes(query); dos.close(); // Read the response DataInputStream dis = new DataInputStream(uc.getInputStream()); String nextline; while ((nextline = dis.readLine()) != null) { System.out.println(nextline); } dis.close(); }
}
</source>
URLConnection: setDoOutput(boolean dooutput)
<source lang="java">
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String args[]) throws Exception { String query = "name=yourname&email=youremail@yourserver.ru"; URLConnection uc = new URL("http:// your form ").openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setAllowUserInteraction(false); DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); // The POST line, the Accept line, and // the content-type headers are sent by the URLConnection. // We just need to send the data dos.writeBytes(query); dos.close(); // Read the response DataInputStream dis = new DataInputStream(uc.getInputStream()); String nextline; while ((nextline = dis.readLine()) != null) { System.out.println(nextline); } dis.close(); }
}
</source>
URLConnection: setIfModifiedSince(long ifmodifiedsince)
<source lang="java">
import java.net.URL; import java.net.URLConnection; import java.util.Date; public class MainClass {
public static void main(String[] args) { try { URLConnection uc = new URL("http://www.demo2s.ru").openConnection(); System.out.println("Will retrieve file if it"s been modified since " + new Date(uc.getIfModifiedSince())); uc.setIfModifiedSince(System.currentTimeMillis()); System.out.println("Will retrieve file if it"s been modified since " + new Date(uc.getIfModifiedSince())); } catch (Exception e) { System.err.println(e); } }
}
</source>
URLConnection: setRequestProperty(String key, String value)
<source lang="java">
import java.io.InputStream; import java.net.URL; import java.net.URLConnection; class MainClass {
public static void main(String[] args) throws Exception { URL url = new URL("http://www.x.ru"); URLConnection urlc = url.openConnection(); urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; " + "Windows NT 5.1; en-US; rv:1.8.0.11) "); InputStream is = urlc.getInputStream(); int c; while ((c = is.read()) != -1) System.out.print((char) c); }
}
</source>
URLConnection: setUseCaches(boolean usecaches)
<source lang="java">
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {
public static void main(String[] args) { URL u; URLConnection uc; try { u = new URL("http://www.jexp.ru"); try { uc = u.openConnection(); if (uc.getUseCaches()) { uc.setUseCaches(false); } } catch (IOException e) { System.err.println(e); } } catch (MalformedURLException e) { System.err.println(e); } }
}
</source>