importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importandroid.annotation.SuppressLint;importandroid.os.StrictMode;importandroid.util.Log;
@SuppressLint("NewApi")public classServiceUtil {public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";public staticString getServiceInfo (String strUrl,String ip,String port)
{
System.setProperty("https.proxyHost", ip);
System.setProperty("https.proxyPort", port);
StrictMode.setThreadPolicy(newStrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(newStrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
HttpURLConnection conn= null;
BufferedReader reader= null;
String rs= null;try{
StringBuffer sb= newStringBuffer();
URL url= newURL(strUrl);
conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(90000);
conn.setReadTimeout(90000);
conn.setInstanceFollowRedirects(false);
conn.connect();
InputStream is=conn.getInputStream();
reader= new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead= null;while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs=sb.toString();
}catch(IOException e) {
Log.e("ServiceUtil-->>getServiceInfo",e.getMessage());
}finally{if (reader != null) {try{
reader.close();
}catch(IOException e) {
Log.e("ServiceUtil-->>getServiceInfo",e.getMessage());
}
}if (conn != null) {
conn.disconnect();
}
}if(null!=rs){
rs= rs.replace( "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "")
.replace("", "")
.replace("", "").replace("\r", "")
.replace("\n", "")
.replace("", "");
}
LogUtil.WriteLog(rs);returnrs;
}/*** post请求获取数据
*@paramstrUrl 请求地址,如: "http://....asmx/HelloWork"
*@paramparams 设置发送的参数,如:"key=1&name=1";//设置发送的参数
*@return
*/
public staticString getServiceInfoPost(String strUrl,String params)
{
String strResult="";try{byte[] entity =params.getBytes();
HttpURLConnection con= (HttpURLConnection) newURL(strUrl).openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setConnectTimeout(90000);
con.setReadTimeout(90000);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",String.valueOf(entity.length));
con.getOutputStream().write(entity);int code =con.getResponseCode();//响应代码 200表示成功
if(code==200){
InputStream inStream=con.getInputStream();
strResult=new String(readInputStream(inStream), "UTF-8");if(null!=strResult){
strResult= strResult.replace( "", "")
.replace("", "")
.replace("", "").replace("\r", "")
.replace("\n", "")
.replace("", "");
}
}
}catch(Exception ex){
Log.e("getServiceInfoPost",ex.getMessage());
}returnstrResult;
}/*** 从输入流中读取数据
*@paraminStream
*@return*@throwsException*/
public static byte[] readInputStream(InputStream inStream) {try{
ByteArrayOutputStream outStream= newByteArrayOutputStream();byte[] buffer = new byte[10000];int len = 0;while( (len = inStream.read(buffer)) !=-1){
outStream.write(buffer,0, len);
}byte[] data = outStream.toByteArray();//网页的二进制数据
outStream.close();
inStream.close();returndata;
}catch(Exception ex){return null;
}
}
}