package com.zking;
import okhttp3.*;
import okhttp3.Authenticator;
import java.io.IOException;
import java.net.*;
public class pnxy {
// 配置代理服务器的地址和端口
public static String proxyHost = "gate.kookeey.io";//主机IP
public static int proxyPort = 15959; // 端口号
public static String username = "kookeey";//账号
public static String password = "12345678";//密码
//socks代理方法
public static void PonxySocks() throws IOException {
// 设置代理服务器的认证凭据
java.net.Authenticator.setDefault(new java.net.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
// 创建代理对象
final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyHost, proxyPort));
// 配置默认代理
ProxySelector.setDefault(new ProxySelector() {
@Override
public java.util.List<Proxy> select(URI uri) {
return java.util.Collections.singletonList(proxy);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
});
// //返回当前代理IP
OkHttpClient client = new OkHttpClient();
String urls = "https://lumtest.com/myip.json"; // 查询代理IP的URL
Request request = new Request.Builder()
.url(urls)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
System.out.println(responseBody);
System.out.println("Request code: " + response.code());
} else {
System.out.println("Request was not successful. Response code: " + response.code());
}
}
//http代理
public static void PonxyHttp() throws IOException {
// 设置代理服务器的认证凭据
Authenticator proxyAuthenticator = new Authenticator() {
@Override
public Request authenticate(okhttp3.Route route, okhttp3.Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
// 创建代理对象
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
// 配置默认代理
OkHttpClient client = new OkHttpClient.Builder()
.proxy(proxy)
.proxyAuthenticator(proxyAuthenticator)
.build();
// 返回当前代理IP
String urls = "https://lumtest.com/myip.json"; // 查询代理IP的URL
Request request = new Request.Builder()
.url(urls)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
System.out.println(responseBody);
System.out.println("Request code: " + response.code());
} else {
System.out.println("Request was not successful. Response code: " + response.code());
}
}
public static void main(String[] args) throws IOException {
PonxySocks(); //Socks
PonxyHttp(); //Http
}
}
pom依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
package com.zking.api.ca.rom;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
public class Font {
//访问网站
public String wz = "https://kookeey.com/";
//开发者密钥
public String keyString = "开发者密钥";
//accessid,开发者ID
public String accessid = "开发者ID";
//hex解析字符串,加密密钥
public String signature = null;
//signature 加密秘钥方法
private String computeSignature(String baseString, String keyString) {
String result = null;
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(baseString.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
result = Base64.getEncoder().encodeToString(hexBytes);
} catch (Exception e) {
}
return result;
}
//处理方法和请求
public String call(String method, HashMap<Object, Object> params) throws Exception {
long tsd = System.currentTimeMillis() / 1000;
String timestamp = String.valueOf(tsd);
Object[] array = params.keySet().toArray();
Arrays.sort(array);
String str = "";
for (int i = 0; i < array.length; i++) {
String key = array[i].toString();
if (i != array.length - 1) {
str = str + key + "=" + URLEncoder.encode(String.valueOf(params.get(key)), "UTF-8")+"&";
} else {
str = str + key + "=" + URLEncoder.encode(String.valueOf(params.get(key)), "UTF-8") ;
}
}
String resurl = "";
if (str!="" && str!=null){
String str2=str+"&ts="+timestamp;
signature=this.computeSignature(str2,keyString);
resurl = wz + method + "?" + "accessid=" + accessid + "&" + "signature=" + signature + "&"+str+ "&"+"ts="+timestamp;
}else {
String str2="ts="+timestamp;
signature=this.computeSignature(str2,keyString);
resurl = wz + method + "?" + "accessid=" + accessid + "&" + "signature=" + signature +"&"+str+"ts="+timestamp;
}
System.out.println(resurl);
return doGet(resurl);
}
// 把浏览器数据封装请求(Get)
public String doGet(String urls) throws IOException{
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
String responseBody= response.toString();
connection.disconnect();
return responseBody;
}
public static void main(String[] args) throws Exception {
Font font=new Font();
String method="stock";
HashMap hashMap =new HashMap();
hashMap.put("g",1);
font.call(method,hashMap);
}
}
pom依赖
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>