Directly paste Code
public class Httpsandhttp {
HTTPS Request implementation
public static StringBuffer Httpssendget ()
{
StringBuffer sb = new StringBuffer ();
String Path = "https://www.12306.cn/";
trustmanager[] Trustallcerts = new trustmanager[]{
New X509trustmanager () {
Public java.security.cert.x509certificate[] Getacceptedissuers () {
return null;
}
public void checkclienttrusted (
Java.security.cert.x509certificate[] certs, String authtype) {
}
public void checkservertrusted (
Java.security.cert.x509certificate[] certs, String authtype) {
}
}
};
try {
Sslcontext sc = sslcontext.getinstance ("SSL");
Sc.init (NULL, Trustallcerts, New Java.security.SecureRandom ());
Httpsurlconnection.setdefaultsslsocketfactory (Sc.getsocketfactory ());
catch (Exception e) {
}
try {
URL url = new URL (path);
Httpsurlconnection conn = (httpsurlconnection) url.openconnection ();
BufferedReader rd = new BufferedReader (New InputStreamReader (Conn.getinputstream (), "UTF-8"));
String Line;
while (line = Rd.readline ())!= null) {
Sb.append (line);
}
Rd.close ();
catch (Malformedurlexception e) {
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return SB;
}
public static void HttpGet (String URL) {
System.out.println (URL);
An example of constructing httpclient
HttpClient httpclient = new HttpClient ();
To create an instance of a Get method
GetMethod GetMethod = new GetMethod (URL);
Use the default recovery policy provided by the system
Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler,
New Defaulthttpmethodretryhandler ());
try {
Execute GetMethod
int statusCode = Httpclient.executemethod (GetMethod);
if (StatusCode!= httpstatus.sc_ok) {
System.err.println ("Method failed:"
+ Getmethod.getstatusline ());
}
Read content
byte[] Responsebody = Getmethod.getresponsebody ();
Handling content
String resposes = new String (responsebody, "utf-8");
SYSTEM.OUT.PRINTLN ("http response:" +resposes);
catch (HttpException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
finally {
Release connection
Getmethod.releaseconnection ();
}
}
public static int HttpPost () {
int i = 1;
String url = "Http://www.baidu.com";
HttpClient httpclient = new HttpClient ();
To create an instance of a Get method
Postmethod Psotmethod = new Postmethod (URL);
Psotmethod.setparameter ("R", "222")//Here Add parameter
Psotmethod.getparams (). Setparameter (Httpmethodparams.http_content_charset, "utf-8");/Request encoding
Use the default recovery policy provided by the system
try {
Execute GetMethod
int statusCode = Httpclient.executemethod (Psotmethod);
if (StatusCode!= httpstatus.sc_ok) {
System.err.println ("Method failed:"
+ Psotmethod.getstatusline ());
}
Read content
byte[] Responsebody = Psotmethod.getresponsebody ();
Handling content
System.out.println (New String (responsebody));
String resposes = new String (responsebody, "utf-8");
SYSTEM.OUT.PRINTLN ("http response:" +resposes);//Get the link to handle it yourself
SYSTEM.OUT.PRINTLN ("http response:" +resposes[1]);
catch (HttpException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return 0;
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return 0;
finally {
Release connection
Psotmethod.releaseconnection ();
}
return i;
}
public static void Main (string[] args) {
HttpPost ();
}
}
Jar Package Address
http://download.csdn.net/detail/huidanyige/8603109