httpclient用法

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;




import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.ibm.wsdl.util.MyX509TrustManager;
import com.sf.core.util.FileUtil;






public String delCacheByDefId(String actDefId){
String serviceUrl = AppConfigUtil.get("serviceUrl");
String ipAddress = AppConfigUtil.get("ipAddress");
if (StringUtil.isEmpty(serviceUrl) || StringUtil.isEmpty(ipAddress))
return "[]";


StringBuffer sb = new StringBuffer();
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:api=\"http://api.webservice.platform.sf.com/\">");
sb.append("   <soapenv:Header/>");
sb.append("   <soapenv:Body>");
sb.append("      <api:delCacheByActDefId>");
sb.append("         <arg0>" + actDefId + "</arg0>");
sb.append("      </api:delCacheByActDefId>");
sb.append("   </soapenv:Body>");
sb.append("</soapenv:Envelope>");


String[] aryIp = ipAddress.split(",");

List<Map<String,Object>> msgs = new ArrayList<Map<String,Object>>();
for (String ip : aryIp) {
Map<String,Object> msg = new HashMap<String, Object>();
String url = serviceUrl.replace("{url}", ip);
try {
HttpUtil.sendMessage(url, sb.toString());
msg.put("ip", ip);
msg.put("status", 0);
msg.put("msg", "");
} catch (IOException e) {
msg.put("status", 1);
msg.put("msg", e.getMessage());
}finally{
msgs.add(msg);
}
}
return JSONArray.fromObject(msgs).toString();
}




/**
* 往指定的URL发送请求。
* @param url
* @param content
* @return
* @throws IOException
*/
public static String sendMessage(String url,String content) throws IOException{
URL uRL;
HttpURLConnection conn;
OutputStream outputStreamWriter = null;
BufferedReader bufferedReader = null;
try {
uRL = new URL(url); 
conn = (HttpURLConnection)uRL.openConnection();
//设置连接超时。
conn.setConnectTimeout(ContextUtil.getConnectTimeout());
//设置读取超时。
conn.setReadTimeout(ContextUtil.getReadTimeout());
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
byte[] sendContents = content.getBytes("utf-8");
conn.setRequestProperty("Content-Length",String.valueOf(sendContents.length));
conn.setRequestProperty("User-Agent","");
outputStreamWriter=conn.getOutputStream();
outputStreamWriter.write(sendContents, 0, sendContents.length); 
outputStreamWriter.flush();
outputStreamWriter.close();

bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer response = new StringBuffer();
String line;
while((line = bufferedReader.readLine()) != null){
response.append(line);
}
String result = response.toString();
return result;
}
catch (MalformedURLException e) {
e.printStackTrace();
return "";
}
catch (IOException e) {
e.printStackTrace();
String msg=ExceptionUtil.getExceptionMessage(e);
log.error(msg);
return "";
}
finally{
if(outputStreamWriter!=null)
outputStreamWriter.close();
if(bufferedReader!=null)
bufferedReader.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值