curl命令转代码
中国
在线工具 计算转换
curl命令转代码

curl命令转代码

eg : curl https://href.lu/

===>to java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

class Main {

public static void main(String[] args) throws IOException {
URL url = new URL("https://href.lu/");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");

InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
}
}

 

相关导航

暂无评论

暂无评论...