请求将现有JavaScript HTTP请求函数转换为Java实现
Got it, let's turn that JavaScript mailExistsCheck function into a Java equivalent! I'll show you two common approaches—one using Apache HttpClient (great for older Java versions) and another using Java 11+'s built-in HttpClient so you don't need external dependencies.
Convert JavaScript
mailExistsCheck to Java Approach 1: Using Apache HttpClient (Recommended for Pre-Java 11)
First, add the Apache HttpClient dependency to your project. If you're using Maven, drop this into your pom.xml:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> <!-- Use the latest stable version available --> </dependency>
Here's the Java function matching your original JS logic:
import org.apache.http.HttpEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class MailChecker { public static void mailExistsCheck() { String targetUri = "https://iag.ksmobile.net/1/cgi/is_exist"; // Auto-close the HttpClient to avoid resource leaks try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost postRequest = new HttpPost(targetUri); // Set all request headers exactly as in your JS function postRequest.setHeader("User-Agent", "FBAndroidSDK.0.0.1"); postRequest.setHeader("Accept-Language", "en_US"); postRequest.setHeader("appid", "135301"); postRequest.setHeader("ver", "3.8.20"); postRequest.setHeader("sid", "17DBB0C2B30D01B590B704501A8AC054"); postRequest.setHeader("sig", "lbOfPDrq0kJNvZX8eYZH9c0Mo7o"); postRequest.setHeader("Host", "iag.ksmobile.net"); // Build multipart form data (the builder handles boundary generation automatically) // Add .addTextBody("fieldName", "value") if you need to include form fields HttpEntity formEntity = MultipartEntityBuilder.create().build(); postRequest.setEntity(formEntity); // Execute the request and process the response httpClient.execute(postRequest, response -> { int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); // Replace with your own response handling logic System.out.println("Request Status Code: " + statusCode); System.out.println("Response Body: " + responseBody); return null; }); } catch (Exception e) { // Add custom error handling here instead of just printing the stack trace e.printStackTrace(); } } public static void main(String[] args) { mailExistsCheck(); } }
Approach 2: Using Java 11+ Native HttpClient
If you're on Java 11 or later, you can use the built-in HttpClient without any external libraries:
import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; public class MailChecker { public static void mailExistsCheck() { String targetUri = "https://iag.ksmobile.net/1/cgi/is_exist"; String boundary = "3i2ndDfv..."; // Use the exact boundary from your JS, or generate a random one // Build the multipart form body (add fields here if needed) String formBody = "--" + boundary + "\r\n" // Example: Add an email field if required by the endpoint // "Content-Disposition: form-data; name=\"email\"\r\n\r\n" // "test@example.com\r\n" + "--" + boundary + "--\r\n"; HttpClient client = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(10)) .build(); HttpRequest postRequest = HttpRequest.newBuilder() .uri(URI.create(targetUri)) .POST(HttpRequest.BodyPublishers.ofString(formBody)) .header("User-Agent", "FBAndroidSDK.0.0.1") .header("Accept-Language", "en_US") .header("appid", "135301") .header("ver", "3.8.20") .header("sid", "17DBB0C2B30D01B590B704501A8AC054") .header("sig", "lbOfPDrq0kJNvZX8eYZH9c0Mo7o") .header("Host", "iag.ksmobile.net") .header("Content-Type", "multipart/form-data; boundary=" + boundary) .build(); try { HttpResponse<String> response = client.send(postRequest, HttpResponse.BodyHandlers.ofString()); System.out.println("Request Status Code: " + response.statusCode()); System.out.println("Response Body: " + response.body()); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { mailExistsCheck(); } }
Key Notes:
- Multipart Boundary: Your original JS had a truncated boundary (
3i2ndDfv...). In the Apache version, the builder auto-generates a valid boundary, but if you need an exact match, use.setBoundary("your-full-boundary")on theMultipartEntityBuilder. - Dynamic Values: The
sidandsigare hardcoded here, but in a real app, these would likely be generated dynamically. Make sure to update them with valid values for your use case. - Error Handling: Both examples use basic exception handling—expand this with custom logic (like retries or user-friendly error messages) based on your app's needs.
内容的提问来源于stack exchange,提问作者pedroribeiro98
相关产品推荐
相关产品推荐

