友善交流技术...

发布新日志

  • java 带证书的https请求

    2016-08-26 16:32:02

    近来需要通过JAVA来实现https请求,普通的忽略证书的https很容易通过,但是带证书的,需要密码的请求,java自带的也可以实现,不过通过网上找的方法一个一个来实,我靠,没有一个能实现的,报了很多的错误如:握手失败的,接收数据失败的。。。。

    最后看到一个文章,是通过apache的SSL客户端来实现的,很方便的,今天记录下来,为以后遇到麻烦的朋友们,解决自己的问题。我是捣鼓了2两天啊。


    java自己带的SSL不太好使用,建议使用apache的SSL请求客户端

    1.下载jar not-yet-commons-ssl-0.3.17.jar 导入项目中 也可以通过pom.xml来实现的

    <!-- https://mvnrepository.com/artifact/org.apache.servicemix.bundles/org.apache.servicemix.bundles.not-yet-commons-ssl -->
    <dependency>
        <groupId>org.apache.servicemix.bundles</groupId>
        <artifactId>org.apache.servicemix.bundles.not-yet-commons-ssl</artifactId>
        <version>0.3.11_1</version>
    </dependency>


    2.导入证书:
       
         1. beta-zh-pc3.client.p12 这个是由安装证书的人提供的。(只要这个证书就可以完成的。上面的jssecacerts 可以不要的)
    2.jssecacerts ,可以通过InstallCert.java 工具生成的(只要输入url地址就可以的如:openapi.xxxx.com,这是在JAVA自带的工具实现时需要的,可以不要jssecacerts)
     
    3.代码的实现如下

     SSLClient client = new SSLClient();
    client.addTrustMaterial(TrustMaterial.DEFAULT);
    client.addTrustMaterial(new KeyMaterial("d:\\ssl\\beta-xxx.client.p12", "xxxxxx".toCharArray()));
    client.setCheckHostname(true); // default setting is "true" for
    client.setCheckExpiry(false); // default setting is "true" for client.setCheckCRL(true); // default setting is "true" for SSLClient
    client.setWantClientAuth(true);
    // Let's load a client certificate (max: 1 per SSLClient instance).
    client.setKeyMaterial(new KeyMaterial("D:\\truststore\\beta-xxx.client.p12", "xxxxxx".toCharArray()));
    URL url = new URL(reqUrl);
    HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
    https.setSSLSocketFactory(client);
    /* 发送数据 */
    https.setDoOutput(true);
    https.setDoInput(true);
    https.setRequestMethod("POST"); //
    https.setRequestProperty("Accept", contType); //
    https.setRequestProperty("Content-Type", contType); //
    https.connect();
    OutputStreamWriter ut = new OutputStreamWriter(https.getOutputStream(), "UTF-8"); //
    out.append(data);
    out.flush();
    out.close();
    /* 接收数据 */
    in = new BufferedReader(new InputStreamReader(
    https.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
    result += line + "\n";
    }
    int code=https.getResponseCode();
    System.out.println(">>>>:Post CODE: "  + code);
  • selenium web自动化初探试

    2013-09-25 15:02:56

    Selenium 经验总结

    安装说明

    Selenium IDE 安装

    下域IDE http://docs.seleniumhq.org/download/previous.jsp 

    然后打开firefox ,加载本组件

    基于FireFox的测试插件中,Selenium IDE是其中的佼佼者。

      Selenium IDE是一个基于FireFoxWeb测试开发环境,可以录制、编辑和调试测试。Selenium IDE包含了Selenium Core,因此可以轻易地在浏览器中录制和回放测试。

      Selenium IDE不仅仅是一个测试录制工具,而是一个IDE,可以录制测试,也可以手工编辑测试,可设置断点进行调试,可把测试保存到HTMLRubyC#Java等其他脚本格式,然后使用Selenium RC来实现并运行更加灵活和强大的测试。

      Selenium IDE下载地址:http://selenium-ide.openqa.org/download.jsp

      下载后,在FireFox中打开插件文件selenium-ide-0.8.7.xpi进行安装,重新启动FireFox后,可选择菜单“Tools | Selenium IDE”打开Selenium IDE的界面。

      录制测试脚本的过程可以用“傻瓜式”来形容,手工编辑脚本是通过选择和插入Selenium命令(Command)的方式来实现的,结合其在线命令帮助(Reference)还是比较容易上手的。可直接在FireFox中运行测试脚本,也可调出TestRunner界面来执行测试脚本。

      Selenium IDE目前仅支持FireFox浏览器,IE中类似的Web测试录制工具目前找到一个“WatiN Test Recorder”,支持在IE中录制WatiN的测试脚本。

     

    Server安装

    下载server

    下载地址:http://docs.seleniumhq.org/download/

    启动server

    D:\ selenium\selenium-2.19.0>java -jar selenium-server-standalone-2.19.0.jar


    录制脚本

    打开firefox浏览器如下

    启动IDE 通过firefox来录制相关的脚本 

     

    解决java实现的问题

    浏览器打开失败

    解决办法

    1)打开java工程属性加载Seleniumjar包,要求最新的jar包应该就可以解决本问题

    2)建议使用webDriver方式来执行脚本

     

    发送tom邮件举例

    package com.example.tests;

    import java.util.regex.Pattern;

    import java.util.concurrent.TimeUnit;

    import org.junit.*;

    import static org.junit.Assert.*;

    import static org.hamcrest.CoreMatchers.*;

    import org.openqa.selenium.*;

    import org.openqa.selenium.firefox.FirefoxDriver;

    import org.openqa.selenium.support.ui.Select;


    public class Tom {

      private WebDriver driver;

      private String baseUrl;

      private boolean acceptNextAlert = true;

      private StringBuffer verificationErrors = new StringBuffer();


      @Before

      public void setUp() throws Exception {

        driver = new FirefoxDriver();

        baseUrl = "http://web.mail.tom.com/";

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

      }


      @Test

      public void testTom() throws Exception {

        driver.get(baseUrl + "/webmail/login/index.action");

        Thread.sleep(2000);

        //tom mail login

        driver.findElement(By.id("username")).clear();

        driver.findElement(By.id("username")).sendKeys("username");

        Thread.sleep(2000);

        driver.findElement(By.id("password3_$loginhash")).clear();

        driver.findElement(By.id("password")).sendKeys(" password");    

        Thread.sleep(2000);

        driver.findElement(By.cssSelector("#login > img")).click();

        Thread.sleep(2000);

        driver.findElement(By.xpath("//ul[@id='ulTabs']/li[3]/span")).click();

        driver.findElement(By.id("pop_close")).click();

        //send mail to xxxxx@tom.com

        driver.findElement(By.id("webmail_sys_write")).click();

        driver.findElement(By.id("tob")).click();

        driver.findElement(By.id("tob")).clear();

        driver.findElement(By.id("tob")).sendKeys("xxxx@tom.com");

        driver.findElement(By.id("subject")).clear();

        driver.findElement(By.id("subject")).sendKeys("zhuti");    

        //send mail content 

        WebElement cont = driver.switchTo().frame(driver.findElement(By.id("xhEdt0_iframe"))).findElement(By.className("editMode"));

        cont.sendKeys("邮件的内容!!!");       

        driver.switchTo().defaultContent();

        

        driver.findElement(By.cssSelector("span[name=\"sendMail\"]")).click();

        driver.findElement(By.id("logout")).click();

        driver.findElement(By.xpath("//button[@type='button']")).click();

        driver.findElement(By.xpath("//button[@type='button']")).click();

      }


      @After

      public void tearDown() throws Exception {

        driver.quit();

        String verificationErrorString = verificationErrors.toString();

        if (!"".equals(verificationErrorString)) {

          fail(verificationErrorString);

        }

      }


      private boolean isElementPresent(By by) {

        try {

          driver.findElement(by);

          return true;

        } catch (NoSuchElementException e) {

          return false;

        }

      }


      private boolean isAlertPresent() {

        try {

          driver.switchTo().alert();

          return true;

        } catch (NoAlertPresentException e) {

          return false;

        }

      }


      private String closeAlertAndGetItsText() {

        try {

          Alert alert = driver.switchTo().alert();

          String alertText = alert.getText();

          if (acceptNextAlert) {

            alert.accept();

          } else {

            alert.dismiss();

          }

          return alertText;

        } finally {

          acceptNextAlert = true;

        }

      }

    }


  • VBS 访问数据库遇到的问题解决办法

    2012-12-04 17:29:30

    今天做VBS入库的操作,但是遇到许多的问题,
     大概如下:语法错误和乱码问题,还有日期时间格式的问题,下面就自己遇到的问题写出如下解决办法。

    1、VBS 访问mysql 基本的代码
    Function mysqlinsert(fconfid,userid,handleid,rev,model,result,comm,picpath,role,testpc)
          Dim conn,strConn,SQL,RS      
          SQL="insert into loginfo(confid,userid,handleid,rev,model,result,comm,picpath,role,testpc) values ('"& fconfid &"','"& userid &"','"& handleid &"','"& rev &"','" & model &"','"& result &"','"& comm &"','"& picpath &"','"& role &"','"& testpc &"');"
     msgbox SQL
          Set conn = CreateObject("ADODB.Connection")
          strConn="DRIVER={MySQL ODBC 5.2w Driver};SERVER=1.1.1.1;DATABASE=xxxx;user id=xxx ; password=xxx"
          conn.ConnectionString=strConn
          conn.Open 
          Set rs= CreateObject("ADODB.Recordset")
          rs.Open SQL,conn
          conn.close
          Set rs=Nothing
          Set conn=Nothing
     
    End Function

    conf ="汉字"
    mysqlinsert conf,11,11,22,11,45,11,11,1,11

    2、如果解决乱码问题
     (1)安装最新的MYSQL ODBC 驱动下载地址:   http://dev.mysql.com/downloads/connector/odbc/5.1.html
     (2)我的数据库版本是 5.2 以上的,建议安装比较最的ODBC驱动
     (3)ODBC参数如下:设置成utf-8,数据库服务器也要设置utf-8
     
    3、提示语法错误如何解决
      一般都是因为在写数据库的时间,没有增加单引号引起的问题如
    正确语句 "insert into loginfo(confid,userid) values ('"& fconfid &"','"& userid"');"
    错误语句 "insert into loginfo(confid,userid) values ("& fconfid &","& userid");"

    4、插入mysq datatime格式不匹配如何解决
      在网上找到下面的函数,可以直接使用,就可以解决格式的问题。
      Function Format_Time(s_Time, n_Flag)
    Dim y, m, d, h, mi, s
    Format_Time = ""
    If IsDate(s_Time) = False Then Exit Function
    y = cstr(year(s_Time))
    m = cstr(month(s_Time))
    If len(m) = 1 Then m = "0" & m
    d = cstr(day(s_Time))
    If len(d) = 1 Then d = "0" & d
    h = cstr(hour(s_Time))
    If len(h) = 1 Then h = "0" & h
    mi = cstr(minute(s_Time))
    If len(mi) = 1 Then mi = "0" & mi
    s = cstr(second(s_Time))
    If len(s) = 1 Then s = "0" & s
    Select Case n_Flag
    Case 1
    ' yyyy-mm-dd hh:mm:ss
    Format_Time = y  & m & d  & h  & mi  & s
    Case 2
    ' yyyy-mm-dd
    Format_Time = y & "-" & m & "-" & d &" " & h & ":" & mi & ":" & s
    Case 3
    ' hh:mm:ss
    Format_Time = h & ":" & mi & ":" & s
    Case 4
    ' yyyy年mm月dd日
    Format_Time = y & "年" & m & "月" & d & "日"
    Case 5
    ' yyyymmdd
    Format_Time = y & m & d
    End Select
    End Function
    '调用函数生成时间
    msgbox  format_time(now(),2)

    VBS 操作数据库这方面的东西还是比较少的,今天搞了一个下午,总算是搞定了。写出来,给不会的人一点指导。

  • tcl 和 shell 实现自动化启动远程shell

    2008-09-11 11:15:48

    1.要安装一个tcl 与 expect 这两个软件.运行环境linux 或是 unix 
      #这个脚本可以传递四个参数: 用户,密码,IP,脚本路径
    #!/usr/bin/expect
    #scrīptName: login_start_command.exp
    proc do_login_console {LoginName PassWord} {
                    expect "*login*"
                    send "$LoginName\n"
                    expect "*Password*"
                    send "$PassWord\n"
                    expect "*]"
                    send_user "\nLogin Successfully!\n"
    }

    proc do_exec_cmd {CommandPath} {
              expect "*]"
              send "sh $CommandPath\n"
              expect "*]"
              send_user "\n\ncommand exec Finished...\n\n"
    }

    if {$argc<4} {
        puts stderr "less parameters...."
        exit 1
    }

    set IpAddress   [lindex $argv 0]
    set LoginName   [lindex $argv 1]
    set PassWord    [lindex $argv 2]
    set CommandPath [lindex $argv 3]

    spawn telnet $IpAddress
    do_login_console $LoginName $PassWord
    do_exec_cmd $CommandPath
    close
    exit 0
     传递参数 IP USRE PWD  执行SHELL脚本的文件.
Open Toolbar