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();
}
}
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();
}
}
相关文章推荐
- 三公网络监督平台APP上线,源代码出售。
- 在没有网络的情况下用安卓手机和数据线让台式电脑上网
- C# 中使用WebClient 请求 https
- 第七章 设计程序架构 之 设计HTTP模块和处理程序
- codeforce 589F(二分 +(贪心 or 网络流))
- angular 采用$http 上传file spring 提示找不到boundary
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- HTTP协议基础
- TCP/IP、Http、Socket的区别
- SCNetworkReachability 说明使用
- HttpClient请求,服务端收到乱码
- net-snmp配置文件snmp.conf
- Android中实现进入App之后检查网络状态
- 简单区分Vmware的三种网络连接模式(bridged、NAT、host-only)
- HTTP报文内的HTTP信息之获取部分内容的范围请求
- 神经网络模型符号解释!!!蛮重要的,记不住的可以参考下。
- 网络爬虫-理解
- IOS Android Tomcat SSL双向认证HTTPS访问
- 关于Xutils网络访问
- A、B、C、D、E类网络地址