HTML 을 문자열 로 변환
4761 단어 자바
, .
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
public class test3 {
@RequestMapping(value = "/static_resources",produces = "text/html; charset=utf-8")
@ResponseBody
public String static_resources( String path) throws KeyManagementException, NoSuchAlgorithmException, IOException {
System.out.println ( "==============static_resources: " + path );
String body = "";
if(null != path) {
// https
SSLContext sslcontext=createIgnoreVerifySSL ();
// http https socket
Registry socketFactoryRegistry=RegistryBuilder. create ()
.register ( "http" , PlainConnectionSocketFactory.INSTANCE )
.register ( "https" , new SSLConnectionSocketFactory ( sslcontext ) )
.build ();
PoolingHttpClientConnectionManager connManager=new PoolingHttpClientConnectionManager ( socketFactoryRegistry );
HttpClients.custom ().setConnectionManager ( connManager );
// httpclient
CloseableHttpClient client=HttpClients.custom ().setConnectionManager ( connManager ).build ();
CloseableHttpResponse response=null;
try {
// get
HttpGet get=new HttpGet ( path );
// Content-type、User-Agent
get.setHeader ( "Content-type" , "application/x-www-form-urlencoded" );
get.setHeader ( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2" );
// , ( )
response=client.execute ( get );
//
HttpEntity entity=response.getEntity ();
if (entity != null) {
// String
body=EntityUtils.toString ( entity , "UTF-8" );
}
EntityUtils.consume ( entity );
System.out.println ( "body:" + body );
} catch (ClientProtocolException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//
if (null != response) {
response.close ();
}
if (null != client) {
client.close ();
}
}
}
return body;
}
public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSLv3");
// X509TrustManager , ,
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[]{trustManager}, null);
return sc;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.