Several realization ways of httpclient

Source: Internet
Author: User
1. Access the Web service using Java native HttpURLConnection.
public class Rtbclient {
	private String url = <a target=_blank href= "http://localhost" >http://localhost</a >;
	Private String port = "8080";
	Private String Path = "/rtbserver/rtbserver";
	
	Public rtbclient () {} public result[] get (dspcontext DC) {if (null = = DC) {return null;
		URL url = null;
		HttpURLConnection urlconn = null;
			try {url = new URL (this.url + ":" + This.port + this.path);
			Urlconn = (httpurlconnection) url.openconnection ();
			Urlconn.setdooutput (TRUE);
			Urlconn.setdoinput (TRUE);
			Urlconn.setusecaches (FALSE);
			Urlconn.setrequestproperty ("Content-type", "Application/x-java-serialized-object");
			Urlconn.setrequestmethod ("POST");
		Urlconn.connect ();
		} catch (Malformedurlexception E1) {e1.printstacktrace ();
		} catch (Protocolexception E1) {e1.printstacktrace ();
		} catch (IOException E1) {e1.printstacktrace ();
			} try {OutputStream outstrm = Urlconn.getoutputstream ();
			ObjectOutputStream oos = new ObjectOutputStream (OUTSTRM);
			Oos.writeobject (DC);
			Oos.flush ();
		Oos.close ();
		} catch (IOException E1) {e1.printstacktrace ();
		} result[] Result; try {InputStream INSTRM = Urlconn.getinputstream ();	
			ObjectInputStream ois = new ObjectInputStream (INSTRM);
		result = (result[]) (Ois.readobject ());
		} catch (ClassNotFoundException e) {e.printstacktrace ();
		} catch (Eofexception e) {e.printstacktrace ();
		} catch (IOException E1) {e1.printstacktrace ();
		} urlconn.disconnect ();
	return null; }
}

2. Use the Apache HttpClient package to access the Web service.

public class Rtbclient {private String address = null;
	private int connreqtimeout = 5000;
	private int conntimeout = 5000;
	private int sotimeout = 5000;
	private int maxconn = 100; 
	Private HttpClient HttpClient = null;

	Poolinghttpclientconnectionmanager Connmanager = null; Public Rtbclient (String address, int connreqtimeout, int conntimeout, int sotimeout, int maxconn) {this.address = Addre
		ss
		This.connreqtimeout = Connreqtimeout;
		This.conntimeout = Conntimeout;
		This.sotimeout = SoTimeout;
		This.maxconn = Maxconn; Requestconfig requestconfig = Requestconfig.custom (). Setconnecttimeout (This.conntimeout).
		Setconnectionrequesttimeout (this.connreqtimeout). SetSocketTimeout (This.sotimeout). build ();
		This.connmanager = new Poolinghttpclientconnectionmanager ();
		This.connManager.setMaxTotal (This.maxconn); This.httpclient = Httpclientbuilder.create (). Setconnectionmanager (This.connmanager). SetDefaultRequestConfig (
	Requestconfig). build (); } public void SendReq (String str) {HttpPost post = new HttpPost (address); 
                  httpentity entity = new stringentity (str);
                  Post.setentity (entity);					
	         HttpResponse response;
		try {
			response = This.httpClient.execute (post);      
			if (null! = response) {
				System.out.println (Response.getstatusline (). Getstatuscode ());
			}
		} catch ( Httphostconnectexception e) {
			logger.error ("Client can not connect server!", e);
			E.printstacktrace ();
		} catch (Exception e) {
			logger.error ("RTB client Get method Invoke exception!", e);
			E.printstacktrace ();
		}
		
		return;
	}
	
}



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.