Send a GET request with parameters and resolve the httpclient encoding problem

Source: Internet
Author: User

Tag: send a GET request with parameters to resolve httpclient encoding problem

Package cn.wemart.httppost;


Import Java.io.BufferedReader;

Import java.io.IOException;

Import Java.io.InputStreamReader;

Import java.io.UnsupportedEncodingException;

Import Java.net.URI;

Import java.net.URISyntaxException;

Import java.util.ArrayList;

Import Java.util.HashMap;

Import java.util.List;

Import Java.util.Map;

Import Net.sf.json.JSONObject;

Import org.apache.http.HttpEntity;

Import Org.apache.http.NameValuePair;

Import org.apache.http.client.ClientProtocolException;

Import org.apache.http.client.entity.UrlEncodedFormEntity;

Import Org.apache.http.client.methods.CloseableHttpResponse;

Import Org.apache.http.client.methods.HttpGet;

Import Org.apache.http.client.methods.HttpPost;

Import org.apache.http.impl.client.CloseableHttpClient;

Import org.apache.http.impl.client.HttpClients;

Import Org.apache.http.message.BasicNameValuePair;

Import Org.apache.http.protocol.HTTP;

Import Org.apache.http.util.EntityUtils;

Import org.dom4j.DocumentException;


public class Executeget {


/**

* Assembly Request Parameters

* @param keyvlauelist

*/

public static map<string,string> Setpostpara (string[][] keyvlauelist) throws documentexception{

map<string,string> map = new hashmap<string,string> ();

for (int i = 0;i<keyvlauelist.length;i++)

{

Map.put (keyvlauelist[i][0],keyvlauelist[i][1]);

}

return map;

}

/**

* Get interface return string

* @param URL

* @throws documentexception

* @throws URISyntaxException

*/

public static string Getgetmethodresponse (String url,string[][] keyvlauelist) {

String Xmlstr =null;

String Str;

try {

Converts a key-value pair into a JSON-formatted string

Map<string, string> Postmap;

Postmap = Setpostpara (keyvlauelist);

Jsonobject object = Jsonobject.fromobject (Postmap);

list<namevaluepair> Postpara = new arraylist<namevaluepair> ();

Postpara.add (New Basicnamevaluepair ("Para", Object.ToString ()));

System.out.println ("+++++para=" +object.tostring () + "+++++");

Send a transcoded string, send

Closeablehttpclient httpclient = Httpclients.createdefault ();

HttpGet httpget = new HttpGet (URL);

String sendstr = entityutils.tostring (new urlencodedformentity (postpara,http. Utf_8));

Httpget.seturi (New URI (Httpget.geturi (). toString () + "?" + Sendstr));

Closeablehttpresponse response = Httpclient.execute (HttpGet);

Handling the Chinese encoding of response

BufferedReader reader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent (), "UTF-8"));

StringBuffer sb = new StringBuffer ();

while ((Str = Reader.readline ()) = null) {

Sb.append (STR). Append ("\ n");

}

Xmlstr = Sb.tostring ();


} catch (Unsupportedencodingexception e) {

E.printstacktrace ();

} catch (Clientprotocolexception e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

} catch (URISyntaxException e) {

E.printstacktrace ();

} catch (Documentexception e) {

E.printstacktrace ();

}

return xmlstr;

}

}


Send a GET request with parameters and resolve the httpclient encoding problem

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.