Java by API/java.net/HttpURLConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:16, 31 мая 2010
Содержание
- 1 HttpsURLConnection: getDefaultSSLSocketFactory()
- 2 HttpURLConnection: getContentLength()
- 3 HttpURLConnection: getContentType()
- 4 HttpURLConnection: getDate()
- 5 HttpURLConnection: getExpiration()
- 6 HttpURLConnection: getHeaderFields()
- 7 HttpURLConnection: getInputStream()
- 8 HttpURLConnection: getLastModified()
- 9 HttpURLConnection: getRequestMethod
- 10 HttpURLConnection: getResponseCode()
- 11 HttpURLConnection: getResponseMessage()
- 12 HttpURLConnection.HTTP_OK
- 13 HttpURLConnection: setInstanceFollowRedirects(boolean followRedirects)
- 14 HttpURLConnection: setRequestMethod(String method) throws ProtocolException
HttpsURLConnection: getDefaultSSLSocketFactory()
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Main {
public static void main(String args[]) throws Exception {
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999);
socket.startHandshake();
SSLSession session = socket.getSession();
java.security.cert.Certificate[] servercerts = session.getPeerCertificates();
List mylist = new ArrayList();
for (int i = 0; i < servercerts.length; i++) {
mylist.add(servercerts[i]);
}
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(mylist);
FileOutputStream f = new FileOutputStream("CertPath.dat");
ObjectOutputStream b = new ObjectOutputStream(f);
b.writeObject(cp);
}
}
HttpURLConnection: getContentLength()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
int len = httpCon.getContentLength();
if (len == -1)
System.out.println("Content length unavailable.");
else
System.out.println("Content-Length: " + len);
}
}
HttpURLConnection: getContentType()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
System.out.println("Content-Type: " + httpCon.getContentType());
}
}
HttpURLConnection: getDate()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
long date = httpCon.getDate();
if (date == 0)
System.out.println("No date information.");
else
System.out.println("Date: " + new Date(date));
}
}
HttpURLConnection: getExpiration()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
long date = httpCon.getExpiration();
if (date == 0)
System.out.println("No expiration information.");
else
System.out.println("Expires: " + new Date(date));
}
}
HttpURLConnection: getHeaderFields()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
Map<String, List<String>> hdrs = httpCon.getHeaderFields();
Set<String> hdrKeys = hdrs.keySet();
for (String k : hdrKeys)
System.out.println("Key: " + k + " Value: " + hdrs.get(k));
}
}
HttpURLConnection: getInputStream()
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
InputStream inStrm = httpCon.getInputStream();
System.out.println("\nContent at " + url);
int ch;
while (((ch = inStrm.read()) != -1))
System.out.print((char) ch);
inStrm.close();
}
}
HttpURLConnection: getLastModified()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
long date = httpCon.getLastModified();
if (date == 0)
System.out.println("No last-modified information.");
else
System.out.println("Last-Modified: " + new Date(date));
}
}
HttpURLConnection: getRequestMethod
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
System.out.println("Request method is " + httpCon.getRequestMethod());
}
}
HttpURLConnection: getResponseCode()
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class BasicAuthNeeded {
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());
}
}
HttpURLConnection: getResponseMessage()
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class Main{
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.google.ru");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
System.out.println("Response Message is " + httpCon.getResponseMessage());
}
}
HttpURLConnection.HTTP_OK
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] argv) throws Exception {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
con.setRequestMethod("HEAD");
System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
}
HttpURLConnection: setInstanceFollowRedirects(boolean followRedirects)
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();
}
}
HttpURLConnection: setRequestMethod(String method) throws ProtocolException
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] argv) throws Exception {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
con.setRequestMethod("HEAD");
System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
}