/ **
* HTTP requests
* @ author kesenhoo
*
* /
public class HttpRequest < / span>
{
public static ; boolean sendXML (String path, String xml) throws Exception
{
byte [] data = xml.getBytes ();
URL url = new URL (path); ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setRequestMethod ( "POST" );
conn.setConnectTimeout ( 5 * 1000 );
/ / If the data submitted via post must be set to allow exporting data <span>
conn.setDoOutput ( true ); < / span>
conn.setRequestProperty ( "Content-Type" , "text / xml; charset = UTF-8" );
conn.setRequestProperty ( "Content-Length" , String.valueOf (data.length));
OutputStream outStream = conn.getOutputStream ();
outStream.write (data);
outStream.flush ();
outStream.close ();
if (conn.getResponseCode () == 200 )
{
return < span class = "keyword"> true ;
}
return false ;
}
/ **
* submitted via get parameter to the server
* @ param path
* @ param params
* @ param enc
* @ return
* @ throws Exception
* /
public static ; boolean sendGetRequest (String path, Map params, String enc) ; throws Exception
{
/ / construct a string of the form, where the situation is different according to the string
/ /? method = save & title = 435435435 & timelength = 89 &
/ / Use StringBuilder object
StringBuilder sb = new StringBuilder (path); ;
sb.append ( '?' ); ;
/ / iterate Map
for (Map.Entry entry : params.entrySet ())
{
sb.append (entry.getKey ()). append ( '= ' )
. append (URLEncoder.encode (entry.getValue (), enc)). append ( '&' );
}
sb.deleteCharAt (sb.length () - 1 ); ;
/ / Open link
URL url = new URL (sb.toString ( ));
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setRequestMethod ( "GET" );
conn.setConnectTimeout ( 5 * 1000 );
/ / if the request response code is 200, it means success
if (conn.getResponseCode () == 200 )
{
return < span class = "keyword"> true ;
}
return false ;
}
/ **
* By Post submitted arguments to the server
* @ param path
* @ param params
* @ param enc
* @ return
* @ throws Exception
* /
public static ; boolean sendPostRequest (String path, Map params, String enc) ; throws Exception
{
/ / need to construct a string of the following form:
/ / title = dsfdsf & timelength = 23 & method = save ;
StringBuilder sb = new StringBuilder ();
/ / If the parameter is not null < / span>
if (params! = null &&! params.isEmpty ())
{
for (Map.Entry entry: params.entrySet ())
{
/ / Post parameters, then submitted You can not omit the content type and length of
sb.append (entry.getKey ()). append ( '=' )
. append (URLEncoder.encode ( entry.getValue (), enc)). append ( '&' );
}
sb.deleteCharAt (sb.length () - 1 );
}
/ / get entity binary data <span> < / span>
byte [] entitydata = sb.toString (). getBytes ();
URL url = new URL (path); ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setRequestMethod ( "POST" );
conn.setConnectTimeout ( 5 * 1000 );
/ / If the data submitted via post must be set to allow exporting data <span>
conn.setDoOutput ( true ); < / span>
/ / here only set the content type and content length header field ;
/ / content-type Content-Type: application / x-www-form-urlencoded
/ / Content-Length Content-Length: 38 < / span>
conn.setRequestProperty ( "Content-Type" , "application / x-www-form-urlencoded" );
conn.setRequestProperty ( "Content-Length" , String.valueOf (entitydata.length));
OutputStream outStream = conn.getOutputStream ();
/ / the entity data is written to the output stream
outStream.write (entitydata);
/ / in-memory data into the brush
outStream.flush ();
outStream.close ();
/ / if the request response code is 200, it means success
if (conn.getResponseCode () == 200 )
{
return < span class = "keyword"> true ;
}
return false ;
}
/ **
* HTTPS secure mode or in the event of a cookie when the operation will be a lot easier to use HTTPclient
* Use HTTPClient (open source project) submitted to the server parameter
* @ param path
* @ param params
* @ param enc
* @ return
* @ throws Exception
* /
public static ; boolean sendRequestFromHttpClient (String path, Map params, String enc) ; throws Exception
{
/ / need to put parameters into NameValuePair
List paramPairs = new ArrayList < NameValuePair> ();
if (params! = null &&! params.isEmpty ())
{
for (Map.Entry entry: params.entrySet ())
{
paramPairs.add ( new BasicNameValuePair (entry.getKey (), entry.getValue ()));
}
}
/ / encode the request parameters, to get the Entity Data <span> < / span>
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity (paramPairs, enc);
/ / construct a request path
HttpPost post = new HttpPost (path); ;
/ / Set the request entity
post.setEntity (entitydata);
/ / browser object
DefaultHttpClient client = new DefaultHttpClient ();
/ / execute post requests
HttpResponse response = client.execute (post);
/ / from the state line to get a status code to determine whether it meets the requirements response code
if (response.getStatusLine (). getStatusCode () == 200 )
{
return < span class = "keyword"> true ;
}
return false ;
}
}
没有评论:
发表评论