5月28
在开发中如果我们遇到这种需要验证的接口应该怎么调用呢?
这里给出一个Basic Authentication 接口调用的工具示例:
package com.heckjj.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class GetAPIResultUtil {
/**
*
*
* @param url
* @param param
* @return
*/
public static String getAPIResult(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
//conn.setConnectTimeout(5000);
String plainCredentials = "heck:12345";
String base64Credentials = new String(Base64.encodeBase64(plainCredentials.getBytes()));
conn.setRequestProperty("Authorization", "Basic " + base64Credentials);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
至此对基本认证(Basic Authentication)的完整过程就基本实现了,希望对大家有所帮助!
来源:Heck's Blog
地址:https://www.heckjj.com/post/540/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
这里给出一个Basic Authentication 接口调用的工具示例:
package com.heckjj.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class GetAPIResultUtil {
/**
*
*
* @param url
* @param param
* @return
*/
public static String getAPIResult(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
//conn.setConnectTimeout(5000);
String plainCredentials = "heck:12345";
String base64Credentials = new String(Base64.encodeBase64(plainCredentials.getBytes()));
conn.setRequestProperty("Authorization", "Basic " + base64Credentials);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
至此对基本认证(Basic Authentication)的完整过程就基本实现了,希望对大家有所帮助!
来源:Heck's Blog
地址:https://www.heckjj.com/post/540/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
一早来打开IDEA,全面
nginx 413 re



