Url post处理
public class UnfiyPushHttpClientUtil {
public static String serviceProvider = "test";
public static String sendSms( String content) {
System.out.println(content);
HttpURLConnection conn = null;
OutputStream os = null;
InputStreamReader isr = null;
String response = "";
try {
try {
URL wsUrl = new URL("http://localhost:9090/smsSendServiceDT/sendSMS");
conn = (HttpURLConnection) wsUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String param = "data=" + URLEncoder.encode(content, "UTF-8")
+ "&serviceProvider=" + URLEncoder.encode(serviceProvider, "UTF-8");
conn.connect();
os = conn.getOutputStream();
os.write(param.getBytes());
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) os.close();
}
int resultCode = conn.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
char[] chr = new char[1024];
isr = new InputStreamReader(conn.getInputStream(),"UTF-8");
int readSize = isr.read(chr);
if (readSize > 0) {
response = new String(chr, 0, readSize);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (isr != null) isr.close();
if (conn != null) conn.disconnect();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return response;
}
}