gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
使用Java代码进行Http的GET请求,请求的地址为https://api.wike.cc/api/senseai/bot,有一个参数为messages,内容为数组
- User
使用Java代码进行Http的GET请求,请求的地址为https://api.wike.cc/api/senseai/bot,有一个参数为messages,内容为数组
- Assistant
可以使用Java的HttpClient库来进行Http的GET请求。以下是一个示例代码:
import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Arrays; public class HttpClientExample { public static void main(String[] args) { // 创建HttpClient对象 HttpClient httpClient = HttpClient.newHttpClient(); // 创建GET请求的URI String url = "https://api.wike.cc/api/senseai/bot"; String[] messages = {"message1", "message2", "message3"}; // 请求参数 String param = Arrays.toString(messages); // 在url中添加参数 try { url += "?messages=" + java.net.URLEncoder.encode(param, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } // 创建GET请求 HttpRequest httpRequest; try { httpRequest = HttpRequest.newBuilder() .uri(new URI(url)) .build(); } catch (URISyntaxException e) { e.printStackTrace(); return; } // 发送GET请求 try { HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } - User
添加一个参数,名为key,值为bQPEFumJrIuZPEyqN569qF5QUo
- Assistant
要添加一个名为
key,值为bQPEFumJrIuZPEyqN569qF5QUo的参数,只需将对应的键值对添加到GET请求的URL中即可。请修改以下代码:import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Arrays; public class HttpClientExample { public static void main(String[] args) { // 创建HttpClient对象 HttpClient httpClient = HttpClient.newHttpClient(); // 创建GET请求的URI String url = "https://api.wike.cc/api/senseai/bot"; String[] messages = {"message1", "message2", "message3"}; // 请求参数 String param = Arrays.toString(messages); // 在url中添加参数 try { url += "?messages=" + URLEncoder.encode(param, "UTF-8"); url += "&key=" + URLEncoder.encode("bQPEFumJrIuZPEyqN569qF5QUo", "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } // 创建GET请求 HttpRequest httpRequest; try { httpRequest = HttpRequest.newBuilder() .uri(new URI(url)) .build(); } catch (URISyntaxException e) { e.printStackTrace(); return; } // 发送GET请求 try { HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }