今天大雪,天冷,不能出去玩了。把保存在pocket里的一些记录总结一下,太懒了。。下篇等我去了上海用我电脑写吧
1.java模拟https登陆
首先我要登陆,然后保存cookie,然后利用cookie来访问后续的网页,发包,处理包这样,然后,为了方便,我选择了 org.apache.http 这个库,典型的一个登陆场景应该是这样的,以后遇到问题一定先要去看官方的例子,别人给出的例子一般要么是不能用,要么是用的方法都是一些过时的,虽然能用,但看到警告还是不舒服。
public static BasicCookieStore cookieStore;
//整个过程用一个client
public static CloseableHttpClient httpclient;
cookieStore = new BasicCookieStore();
httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
//先访问一下首页,然后cookie、会填充进来
HttpGet httpget = new HttpGet("https://leaver.me/index.htm");
httpclient.execute(httpget);
CloseableHttpResponse responseCookie = null;
//然后post请求登陆
HttpPost httpost = new HttpPost("https://leaver.me/login.htm");
//通过键值对来作为post参数
List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (1);
nameValuePairs.add(new BasicNameValuePair("loginType", "1"));
nameValuePairs.add(new BasicNameValuePair("loginName", username));
nameValuePairs.add(new BasicNameValuePair("Password", password));
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));
CloseableHttpResponse responLogin = null;
responLogin = httpclient.execute(httpost);
但是,这个过程报了如下的错
e: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
google之,发现时访问https的原因。需要先安装对应站点的证书。这里要用到一个通用的java类,我贴下链接
编译这个工具类,然后执行java InstallCert <host>[:port] 会生成一个证书文件
然后在项目里通过
System.setProperty("javax.net.ssl.trustStore",
"证书路径");
设置即可,你可以吧证书文件放到资源目录,就更好了。
2.java正则替换反斜线
我在某个地方需要把字符串里的所有反斜线替换成两个,我就写了
str.replaceAll("\\","\\\\")
结果发现我还是too young,实际上,
java replaceAll() 方法要用 4 个反斜杠,表示一个反斜杠 例如
str1="aa\bbb";
要想替换成
str1="aa\\bbb";
必须这样替换:
str1 = str1.replaceAll("\\\\", "\\\\\\\\");
原因如下: String 的 replaceAll() 方法,实际是采用正则表达式的规则去匹配的, \\ ,java解析为\交给正则表达式, 正则表达式再经过一次转换,把\转换成为\ 也就是java里面要用正则来表示一个. 必须写成4个\ 如果要表示\,那就要写8个\ 所以如果写成: str1 = str1.replaceAll("\", “\\”); 就会报正则表达式的错误。
3.httpClient如何模拟表单上传文件
这个还是要去看官方例子,网上没找到,需要添加
org.apache.http.entity包
//文件部分
FileBody csvFile = null;
//表单的其他部分
StringBody filelog = null;
StringBody dataItemDefine = null;
csvFile = new FileBody(new File(file));
filelog = new StringBody("ADD", ContentType.TEXT_PLAIN);
dataItemDefine = new StringBody(GlobalSetting.getValueOfKey(type), ContentType.TEXT_PLAIN);
//关键代码,此处来构造请求数据
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("filelog", changeLogAction).addPart("dataItemDefine", dataItemDefine).addPart("fileName", csvFile).build();
HttpPost httppost = new HttpPost("https://leaver.me/uploadFile.action");
httppost.setEntity(reqEntity);
CloseableHttpResponse response = null;
response = httpclient.execute(httppost);
其他都和普通的post请求没啥区别了
4.json-lib的一个问题
在maven里添加了json-lib的依赖后,启动报错
missing net.sf.json-lib,但实际上这个我的确是加载进来了。去了SO,才知道,还需要知道classifier 依赖,把jdk的版本添加上就行。这个应该是我当时添加的时候没有去maven中央库填写造成的
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
5.maven生成可执行jar包
写好的工具要打包,需要在pom文件,(我用mave来管理)里添加如下的配置
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>*.account.core.GenerateAccount</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
然后在工程目录执行
mvn assembly:assembly
就会在target目录生成jar包啦