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