public class HttpUtil {
public static String executePost(String url, List<NameValuePair> list) {
HttpPost post = new HttpPost(url);
if (list != null && list.size() > 0) {
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
HTTP.UTF_8);
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
return result;
} else {
return "-2";
}
} catch (Exception e) {
e.printStackTrace();
}
return "-2";
}
public static String executeGet(String url, Map<String, String> params, String encoding) throws Exception {
StringBuilder sb = new StringBuilder(url);
sb.append('?');
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=').append(
URLEncoder.encode(entry.getValue(), encoding)).append('&');
}
sb.deleteCharAt(sb.length() - 1);
URL url2 = new URL(sb.toString());
HttpGet get = new HttpGet(url2);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
return result;
} else {
return "-2";
}
} catch (Exception e) {
e.printStackTrace();
}
return "-2";
}
HttpURLConnection 方式:
=====================
public void sendSms() throws Exception{
String message="货已发到";
message=URLEncoder.encode(message, "UTF-8");
System.out.println(message);
String path ="http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+message;
URL url =new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = StreamTool.readInputStream(inStream);
String result=new String(data, "UTF-8");
System.out.println(result);
}
HttpURLConnection方式:
public static String uploads(String xml, String path, String authorization) {
try {
byte[] data = xml.getBytes("UTF-8");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(8 * 1000);
conn.setReadTimeout(8 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Authorization", authorization);
conn.setRequestProperty("Content-Type",
"application/json; charset=UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
StringBuffer buf = new StringBuffer();
String s = "";
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
while ((s = br.readLine()) != null) {
buf.append(s);
}
br.close();
in.close();
}
return buf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "-2";
}
}