您的位置:首页 > 理论基础 > 计算机网络

httpclient用法

2015-10-29 18:09 423 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: