Skip to content

Commit

Permalink
feat: 兼容 https(SSL) 接口
Browse files Browse the repository at this point in the history
旧版本的作者没有考虑https,此版本兼容了https
  • Loading branch information
renfei committed Mar 4, 2021
1 parent a2119c6 commit bcc05c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ GPLv2具有很强的传染性,选用这个开源协议并不是我定的,而
<dependency>
<groupId>net.renfei</groupId>
<artifactId>discuz-ucenter-api-for-java</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```

如果你使用的是传统方式,需要下载```Jar```包,放入```lib```文件夹,下载地址:[discuz-ucenter-api-for-java-1.0.4.jar
](https://github.com/renfei/discuz-ucenter-api-for-java/releases/download/1.0.4/discuz-ucenter-api-for-java-1.0.4.jar)
如果你使用的是传统方式,需要下载```Jar```包,放入```lib```文件夹,下载地址:[discuz-ucenter-api-for-java-1.0.5.jar
](https://github.com/renfei/discuz-ucenter-api-for-java/releases/download/1.0.5/discuz-ucenter-api-for-java-1.0.5.jar)

## 使用

为了跟```SpringBoot```更好的集成,我对原项目进行了修改,原项目是修改配置文件,我修改为了实例化时传递参数的方式

## Https(SSL) 站点
如果你的站点是```https```的,必须使用版本号```1.0.5```以上,旧版本的作者没有考虑```https```,我修改了代码兼容了```https```

### 实例化

实例化一个客户端```net.renfei.discuz.ucenter.client.Client```,参数依次是:UCenter接口地址、IP地址、通讯Key、APPID、Connect。
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.renfei</groupId>
<artifactId>discuz-ucenter-api-for-java</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<packaging>jar</packaging>
<name>discuz-ucenter-api-for-java</name>
<url>https://github.com/renfei/discuz-ucenter-api-for-java</url>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/net/renfei/discuz/ucenter/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.renfei.discuz.ucenter.client;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -317,7 +319,7 @@ protected String ucFopen2(String url, int limit, String post, String cookie, boo
protected String ucFopen(String url, int limit, String post, String cookie, boolean bysocket, String ip, int timeout, boolean block) {
StringBuilder returnString = new StringBuilder();

URL matches;
URL matches = null;
String host = "";
String path = "";
int port = 80;
Expand All @@ -337,7 +339,7 @@ protected String ucFopen(String url, int limit, String post, String cookie, bool

StringBuffer out = new StringBuffer();
if (post != null && post.length() > 0) {
out.append("POST ").append(path).append(" HTTP/1.0\r\n");
out.append("POST ").append(path).append(" HTTP/1.1\r\n");
out.append("Accept: */*\r\n");
out.append("Accept-Language: zh-cn\r\n");
out.append("Content-Type: application/x-www-form-urlencoded\r\n");
Expand All @@ -349,7 +351,7 @@ protected String ucFopen(String url, int limit, String post, String cookie, bool
out.append("Cookie: \r\n\r\n");
out.append(post);
} else {
out.append("GET path HTTP/1.0\r\n");
out.append("GET path HTTP/1.1\r\n");
out.append("Accept: */*\r\n");
//out .= "Referer: boardurl\r\n";
out.append("Accept-Language: zh-cn\r\n");
Expand All @@ -360,7 +362,13 @@ protected String ucFopen(String url, int limit, String post, String cookie, bool
}

try {
Socket fp = new Socket(ip != null && ip.length() > 10 ? ip : host, port);
Socket fp = null;
String socketHost = ip != null && ip.length() > 10 ? ip : host;
if ("https".equals(matches.getProtocol().toLowerCase())) {
fp = SSLSocketFactory.getDefault().createSocket(socketHost, port);
} else {
fp = new Socket(socketHost, port);
}
if (!fp.isConnected()) {
System.out.println("net.renfei.discuz.ucenter.client.Client.ucFopen:\n"
+ "Socket Not Connected\n");
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public class Test {
* @param args
*/
public static void main(String[] args) {
Client uc = new Client("http://localhost/uc_server",null,"123456","3","");
// testLogin(uc);
// Client uc = new Client("https://bbs.renfei.net/uc_server",null,"123456","2","");
Client uc = new Client("https://bbs.afuiot.com/uc_server",null,"123456","6","");
testLogin(uc);
// synlogin(uc);
String s = uc.ucAuthcode("8485m7QEfsvnOg9tKGvAsxlwXpAzZU6LhFA04pD6N0XIYG1cJVDh2Th83Qcci130UPHbXp+UNG0","DECODE");
System.out.println(s);
Expand All @@ -40,7 +41,7 @@ public static void synlogin(Client client){
}

public static void testLogin(Client client){
String result = client.ucUserLogin("renfei", "password");
String result = client.ucUserLogin("renfei", "1123");

LinkedList<String> rs = XMLHelper.ucUnserialize(result);
if(rs.size()>0){
Expand Down

0 comments on commit bcc05c2

Please sign in to comment.