Java HTTP GET POST request HTTPS request implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.