import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.base64;public class HttpBasicAuth { public static void main(String[] args) { try { URL url = new URL ("http://ip:port/login"); String encoding = base64.getEnprer().enpreToString(("test1:test1").getBytes("UTF-8")); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty ("Authorization", "Basic " + encoding); InputStream content = (InputStream)connection.getInputStream(); BufferedReader in = new BufferedReader (new InputStreamReader (content)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } catch(Exception e) { e.printStackTrace(); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)