您好,欢迎访问三七文档
packagecom.ycnet.mirage.integration.service.impl;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.StringWriter;importjava.net.HttpURLConnection;importjava.net.InetSocketAddress;importjava.net.Proxy;importjava.net.URL;importjava.security.SecureRandom;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;importjavax.net.ssl.HostnameVerifier;importjavax.net.ssl.HttpsURLConnection;importjavax.net.ssl.KeyManager;importjavax.net.ssl.SSLContext;importjavax.net.ssl.SSLSession;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;/***WebClientWeb客户端**@author©union.home**/publicclassWebClient{/***是否使用代理服务器*/privatebooleanuseProxy=false;/***代理服务器地址*/privateStringproxyHost;/***代理服务器端口*/privateintproxyPort;/***代理服务器认证用户名*/privateStringproxyUserName;/***代理服务器认证密码*/privateStringproxyPassword;/*publicstaticvoidmain(String[]args){Stringhost=10.193.11.49;intport=80;WebClientclient=newWebClient();try{Stringdata=txnType=72&userMac=&riskLevel=&bindId=&merId=435410160120000&subMerName=&bindInfo=&txnSubType=01&reserved=&customerInfo=ezAxfDQxMDQ4MjE5ODYwODE5Mzg1OXzpl6vluIXotoV8fHx8fH0%3D&version=3.0.0&accType=01&accNo=6222980100596461&certId=114247947639323227832053018078378478405&merType=0&encoding=UTF-8&bizType=000000&reqReserved=&subMerId=&encryptCertId=74514726712890552725661682850992756359&signature=hEq0eFoZHTdVH2lW1FhQ95YzEe5hJdEoRsR4II5gc%2BerZRRpPq1bekJiap7zf1gCPzTvF%2FQqdCpW5k4cziVEhA6%2Fgz5cQBiKNbw9VI30LqFW6MO%2FOTBiXX9ONe3xsTgvEACfunqylYZyhS%2BwVkd6SFxH2q884ICT9UUtXjsFvw0%3D&orderId=201501201837311135123ce&accessType=0&txnTime=20150120183731;System.out.println(27.115.104.182requestdata:\n+data);Stringaaa=client.doPost();System.out.println(27.115.104.182responseresult:\n+aaa);}catch(Exceptionex){ex.printStackTrace();}}*/publicWebClient(){}/***带参数构造函数**@paramproxyHost*代理服务器地址*@paramproxyPort*代理服务器端口*@paramproxyUserName*代理服务器认证用户名*@paramproxyPassword*代理服务器认证密码*/publicWebClient(StringproxyHost,intproxyPort,StringproxyUserName,StringproxyPassword){this.useProxy=true;this.proxyHost=proxyHost;this.proxyPort=proxyPort;this.proxyUserName=proxyUserName;this.proxyPassword=proxyPassword;}/***设置代理服务器信息**@paramproxyHost*代理服务器地址*@paramproxyPort*代理服务器端口*@paramproxyUserName*代理服务器认证用户名*@paramproxyPassword*代理服务器认证密码*/publicvoidsetProxy(StringproxyHost,intproxyPort,StringproxyUserName,StringproxyPassword){this.useProxy=true;this.proxyHost=proxyHost;this.proxyPort=proxyPort;this.proxyUserName=proxyUserName;this.proxyPassword=proxyPassword;}/***以POST方式发送数据**@paramurl*请求URL*@paramcontent*发送内容*@paramcharset*编码字符集*@paramconnectionTimeout*连接超时时间*@paramreadTimeout*读取数据超时时间*@return*@throwsException*/publicStringdoPost(Stringurl,Stringcontent,StringcontentType,Stringcharset,intconnectionTimeout,intreadTimeout)throwsException{//StringcontentType=application/json;charset=+charset;contentType=contentType+;charset=+charset;returndoPost(url,content.getBytes(charset),contentType,connectionTimeout,readTimeout);}/***以POST方式发送数据**@paramurl*请求URL*@paramcontent*发送内容*@paramcontentType*内容类型*@paramconnectionTimeout*连接超时时间*@paramreadTimeout*读取数据超时时间*@return*@throwsException*/publicStringdoPost(Stringurl,byte[]content,StringcontentType,intconnectionTimeout,intreadTimeout)throwsException{HttpURLConnectionhttpConn=null;OutputStreamout=null;Stringresult=null;try{httpConn=getConnection(newURL(url));httpConn.setConnectTimeout(connectionTimeout);//连接超时时间httpConn.setReadTimeout(readTimeout);//读取结果超时时间httpConn.setRequestProperty(Content-type,contentType);httpConn.setRequestMethod(POST);out=httpConn.getOutputStream();out.write(content);result=getResponseAsString(httpConn);}catch(IOExceptionex){System.out.println(IOException:+ex.getMessage());throwex;}catch(Exceptionex){System.out.println(Exception:+ex.getMessage());throwex;}finally{if(out!=null){out.close();}if(httpConn!=null){httpConn.disconnect();}}returnresult;}/***发送GET请求**@paramurl*请求URL*@paramcharset*编码字符集*@return*@throwsException*/publicStringdoGet(Stringurl,StringcontentType,Stringcharset)throwsException{HttpURLConnectionhttpConn=null;Stringresult=null;try{contentType=contentType+;charset=+charset;//StringcontentType=application/x-=//+charset;httpConn=getConnection(newURL(url));httpConn.setRequestProperty(Content-type,contentType);httpConn.setRequestMethod(GET);result=getResponseAsString(httpConn);}catch(Exceptionex){System.out.println(Exception:+ex.getMessage());throwex;}finally{if(httpConn!=null){httpConn.disconnect();}}returnresult;}/***获取连接**@paramurl*请求URL*@return*@throwsIOException*@throwsException*/privateHttpURLConnectiongetConnection(URLurl)throwsIOException,Exception{HttpURLConnectionhttpConn=null;try{if(this.useProxy){//使用代理服务器Proxyproxy=newProxy(Proxy.Type.HTTP,newInetSocketAddress(this.proxyHost,this.proxyPort));httpConn=(HttpURLConnection)url.o
本文标题:WebClient
链接地址:https://www.777doc.com/doc-5535775 .html