Fetion test httpClient for free Send interface API

Source: Internet
Author: User

Test fetion free Send interface API test (HttpClient implementation)

Advantages of Use: fast and convenient

Use disadvantage: The user's various information in the clear form of transmission in the network is not secure.

For testing only

Package cn.com.vnvtrip.fection.sms.utils;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;

/**
* Read the configuration information of fection
*
* @author Longgangbai
*
*/
public class Env {

private static Logger Logger = Logger.getlogger (Env.class.getName ());
private static Properties props = null;
private static ENV env = NULL;

static {
Get classpath path configuration sydney sex
File File = new file (env.class
. GetResource ("/fectionservice.properties"). GetFile ());

if (file.exists ()) {
try {
InputStream in = new FileInputStream (file);
props = new Properties ();
Props.load (in);
} catch (IOException e) {
Logger.log (level.warning, "Load fection configuration failed!");
}
} else {
Logger.log (level.warning, "fection configuration file does not exist, please check:");
}
}

/**
*
*/
Private Env () {
}

/**
* Prevents asynchronous behavior from getting configuration information
*
*/
public static synchronized Env GetEnv () {
if (env = = null) {
env = new env ();
}
return env;
}

/**
* Get the Properties of the configuration
*
* @return
*/
Public Properties getProperties () {
return props;
}

public static void Main (string[] args) {
System.out.println (Env.class.getResource ("/fectionservice.properties")
. GetFile ());
}
}

Package cn.com.vnvtrip.fection.sms.utils;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;

Import Org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
Import Org.apache.commons.httpclient.Header;
Import org.apache.commons.httpclient.HttpClient;
Import org.apache.commons.httpclient.HttpException;
Import Org.apache.commons.httpclient.HttpStatus;
Import Org.apache.commons.httpclient.NameValuePair;
Import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
Import Org.apache.commons.httpclient.methods.GetMethod;
Import Org.apache.commons.httpclient.methods.PostMethod;
Import Org.apache.commons.httpclient.params.HttpMethodParams;

/**
*
* @author Longgangbai
*
*/
public class HTTPClient {
/**
*
* @param httpurl
* @return
*/
public static string Execute (string httpurl) {
Application of building an instance of HttpClient
HttpClient HttpClient = new HttpClient ();
Create an instance of the Get method
GetMethod GetMethod = new GetMethod (Httpurl);
Create an instance of the Post method Postmethod postmethod=new Postmethod (Httpurl);

Using the system-provided default recovery policy
Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler,
New Defaulthttpmethodretryhandler ());
try {
instance of the request executing the GET method
int statusCode = Httpclient.executemethod (GetMethod);
How to view the encoding of a response
String responseencoding = Getmethod.getrequestcharset ();
SYSTEM.OUT.PRINTLN ("The Response encoding is:" + responseencoding);
Detects if the send was successful
if (statusCode! = HTTPSTATUS.SC_OK) {
System.out.println ("Method failure:"
+ Getmethod.getstatusline ());
}
The message body that gets the response
byte[] Responsebody = Getmethod.getresponsebody ();
return new String (responsebody);
} catch (HttpException e) {
System.out.println ("Please check provided HTTP address! "+ e);
} catch (IOException e) {
Network exception information occurred
E.printstacktrace ();
} finally {
Release connection
Getmethod.releaseconnection ();
}
return null;

}

/**
* Using HttpClient to invoke a remote servlet
*
* @param httpurl
* @param xmlinfo
* @param map
* @return
*/
@SuppressWarnings ("Unchecked")
public static InputStream Executehttp (String httpurl, String xmlinfo,
Map<string, string> map) {
HttpClient HttpClient = new HttpClient ();
Application of the method of sending messages using post
Postmethod Postmethod = new Postmethod (Httpurl);
Bytearrayrequestentity requestentity = new Bytearrayrequestentity (
Xmlinfo.getbytes (), "text/html; Charset=utf-8 ");
InputStream inputstream = null;
Set the requested entity
Postmethod.setrequestentity (requestentity);
Format the request
try {
Set Message header information
if (map! = null) {
for (Iterator it = Map.entryset (). Iterator (); It.hasnext ();) {
Entry<string, string> Header = (entry<string, string>) it
. Next ();
String key = Header.getkey ();
String value = Header.getvalue ();
Postmethod.setrequestheader (key, value);
}
}
Of the method that sent the message.
Httpclient.executemethod (Postmethod);

Send a message that successfully accepted the request
if (postmethod.getstatuscode () = = HTTPSTATUS.SC_OK) {
InputStream = Postmethod.getresponsebodyasstream ();
} else {
SYSTEM.OUT.PRINTLN ("Unexpected failure:"
+ Postmethod.getstatusline ());
}

} catch (HttpException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
Postmethod.releaseconnection ();
}
return inputstream;
}

/**
* Using HttpClient to invoke a remote servlet
*
* @param httpurl
* @param xmlinfo
* @param map
* @return
*/
@SuppressWarnings ("Unchecked")
public static InputStream executehttp (String Httpurl,
Map<string, string> parammaps, map<string, string> Map) {
HttpClient HttpClient = new HttpClient ();
Application of the method of sending messages using post
Postmethod Postmethod = new Postmethod (Httpurl);
InputStream inputstream = null;
Sets the value of the requested fill in individual form fields
list<namevaluepair> paramlist = new arraylist<namevaluepair> ();
namevaluepair[] params = new namevaluepair[parammaps.size ()];
if (parammaps! = null) {
for (Iterator it = Parammaps.entryset (). Iterator (); It.hasnext ();) {
Entry<string, string> Header = (entry<string, string>) it
. Next ();
String key = Header.getkey ();
String value = Header.getvalue ();
Namevaluepair param = new Namevaluepair ();
Param.setname (key);
Param.setvalue (value);
Paramlist.add (param);
}
}
Paramlist.toarray (params);
Postmethod.setrequestbody (params);
Format the request
try {
if (map! = null) {
Set Message header information
for (Iterator it = Map.entryset (). Iterator (); It.hasnext ();) {
Entry<string, string> Header = (entry<string, string>) it
. Next ();
String key = Header.getkey ();
String value = Header.getvalue ();
Postmethod.setrequestheader (key, value);
}
}
Of the method that sent the message.
int statusCode = Httpclient.executemethod (Postmethod);
Application of the automatic steering method
HttpClient for requests to receive subsequent services, such as post and put, cannot be handled automatically forwarding 301 or 302
if (StatusCode = = httpstatus.sc_moved_permanently
|| StatusCode = = httpstatus.sc_moved_temporarily) {
Header Locationheader = Postmethod.getrequestheader ("location");
String location = null;
if (Locationheader! = null) {
Location = Locationheader.getvalue ();
System.out
. println ("The page was redirected to:" + location);

} else {
System.out.println ("Location field, value is null! ");
}
}
Send a message that successfully accepted the request
if (postmethod.getstatuscode () = = HTTPSTATUS.SC_OK) {
InputStream = Postmethod.getresponsebodyasstream ();
} else {
SYSTEM.OUT.PRINTLN ("Unexpected failure:"
+ Postmethod.getstatusline ());
}
} catch (HttpException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
Postmethod.releaseconnection ();
}
return inputstream;
}
}
Package cn.com.vnvtrip.fection.sms.service;

Import Java.io.InputStream;
Import Java.util.Map;

Import cn.com.vnvtrip.fection.sms.utils.HTTPClient;

public class SMSService {
private static SMSService SMSService = null;

Private SMSService () {

}

public static SMSService Getsmsservice () {
if (SMSService = = null) {
SMSService = new SMSService ();
}
return smsservice;
}

/**
*
* @param httpurl
* @return
*/
public string Execute (string httpurl) {
Return Httpclient.execute (Httpurl);
}

/**
* Using HttpClient to invoke a remote servlet
*
* @param httpurl
* @param xmlinfo
* @param map
* @return
*/
@SuppressWarnings ("Unchecked")
Public InputStream executehttp (string httpurl, String xmlinfo,
Map<string, string> map) {
Return Httpclient.executehttp (Httpurl, xmlinfo, map);
}

/**
* Using HttpClient to invoke a remote servlet
*
* @param httpurl
* @param xmlinfo
* @param map
* @return
*/
@SuppressWarnings ("Unchecked")
Public InputStream executehttp (String Httpurl,
Map<string, string> parammaps, map<string, string> Map) {
Return Httpclient.executehttp (Httpurl, parammaps, map);
}
}

Fetion test httpClient for free Send interface API

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.