在华为实现的java访问https,多线程,写日志

package IVRInter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;

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

//供IVR调用的java接口
public class CopyOfMyHTTPS {
public static int seq=0;
public static int maxNum=10; //是个日志文件,0-9
public static int maxLength=1024*10240;//文件长度10M
//public static int ttt=10000;

public synchronized static int requestHTTPS(Pojo p,String url,String proxy,int port,final String username,
  final String password) throws KeyManagementException, IOException {//synchronized 避免多线程访问文件,异常
//记日志
String logStr="<1>url:"+getString(url)+" proxy:"+getString(proxy)
+" port:"+port+" username:"+username+" password:"+getString(password)+"/n";
writeToFile2(logStr);

//System.out.println("1金额>>"+ ttt);
//ttt-=10;
//System.out.println("2余额>>"+ ttt);
//判断输入参数
if(url.length()>1024) return 1;
if(proxy.length()>1024) return 2;


boolean proxyFlag= false;
if(!(proxy == null ||"".equals(proxy))){//用代理时
initProxy(proxy,port,username,password);
proxyFlag= true;
}
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
//指定信任https
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);

// 记日志
logStr="<2>url:"+getString(url)+" proxy:"+getString(proxy)
+" port:"+port+" username:"+username+" password:"+getString(password)+"/n";
writeToFile2(logStr);
 
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
String mes="";
try {
mes = conn.getResponseMessage();
System.out.println("返回结果:"+mes);
} catch (Exception e) {
return 3;
}
if(!("OK".equals(mes))){
if(proxyFlag){
return 4;//https地址及代理信息是否正确
}else{
return 5;//https地址是否正确
}
}
conn.connect();

//记日志
logStr="<3>url:"+getString(url)+" proxy:"+getString(proxy)
+" port:"+port+" username:"+username+" password:"+getString(password)+"/n";
writeToFile2(logStr);


InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String curLine="";
String contend="";
while ((curLine = reader.readLine()) != null) {
contend+=curLine;
//System.out.println(curLine);
}
p.res = contend;
is.close();


//记日志
logStr="<4>httpReturn:"+ getString(p.res)+" url:"+getString(url)+" proxy:"+getString(proxy)
+" port:"+port+" username:"+username+" password:"+getString(password)+"/n";
writeToFile2(logStr);


SimpleDateFormat sf=new SimpleDateFormat("HH:mm:ss");
System.out.println(sf.format(new Date()));

if(conn.getResponseCode()==200){
return 0;
}else{
if(proxyFlag){
return 6;
}else{
return 7;
}
}
}

//写文件
public static void writeToFile(File file,String str){  
BufferedWriter pw=null;
try {
pw=new BufferedWriter(new FileWriter(file.getAbsolutePath(),true));
pw.write(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}  
public static String getString(String str){
 if(str.length()>6){
 return str.substring(0,4)+"*"+str.substring(str.length()-2);
 }
 return str;
 }
 //写文件(判断是否切换)
 //路径待完善,到时添加一个输入参数 TODO。。
 public static void writeToFile2(String str){  
 File file = new File("HWHTTP_301_"+seq+".log");
 if(file.length()+str.getBytes().length > maxLength){
 seq++;
 if(seq >= maxNum){
 seq = 0;
 }
 file = new File("HWHTTP_301_"+seq+".log");
 if(file.exists()){
 file.delete(); 
 file = new File("HWHTTP_301_"+seq+".log");
 }
 }
 writeToFile(file,str);
 }  
 
 //初始化代理
 public static void initProxy(String host, int port, final String username,
 final String password) {
 Authenticator.setDefault(new Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(username,
 new String(password).toCharArray());
 }
 });
 System.setProperty("proxyType", "4");
 System.setProperty("proxyPort", Integer.toString(port));
 System.setProperty("proxyHost", host);
 System.setProperty("proxySet", "true");
 }
 private static class TrustAnyTrustManager implements X509TrustManager {
 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 }
 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 }
 public X509Certificate[] getAcceptedIssuers() {
 return new X509Certificate[]{};
 }
 }
 private static class TrustAnyHostnameVerifier implements HostnameVerifier {
 public boolean verify(String hostname, SSLSession session) {
 return true;
 }
 }
 }
 class Pojo{
 String res;
 }

 

----------------------------------------------------------------------------------------------------------------

测试类:

package IVRInter;

import java.io.IOException;
import java.security.KeyManagementException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test extends Thread{
 public static Pojo p=new Pojo();
 
 @Override
 public void run() {
   try {
   int a=new MyHTTPS().requestHTTPS(p, "https://www.mpower-shipper.com/MPS/Home.jsp", "openproxy.huawei.com", 8080, null, null);
  } catch (KeyManagementException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
   
  
 }

 public static void main(String[] args) throws Exception{

  SimpleDateFormat sf=new SimpleDateFormat("HH:mm:ss");
  System.out.println(sf.format(new Date()));
   for(int i=0;i<10000;i++){
    new Test().start();
   }
 }

}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值