gpt4 book ai didi

Java Jersey-客户端 header 问题

转载 作者:行者123 更新时间:2023-11-30 07:06:39 25 4
gpt4 key购买 nike

我正在尝试将以下 header 添加到其余的 Post 调用中...它可以在纯 Java 中工作,但我正在尝试使用 Jersey 客户端库重写它...当我使用 Jersey 发布帖子时我收到一个 API 文档中未列出的错误代码,所以我知道这一定只是一个小问题,例如缺少 header ...知道我在底部函数中做错了什么吗?

普通 Java 添加 header 功能有效:

private void SetDefaultHeaders(HttpURLConnection conn) {
setRequestProperty(conn, "Accept", "*");
setRequestProperty(conn, "Content-Type", "application/x-www-form-urlencoded");
}

Jersey 代码:

public void logIn(String email, String password) {
if (email != "" && email != null && password != "" && password != null) {
try {
StringBuilder sb = new StringBuilder();
sb.append(Settings.WIFIPLUG_URL);
sb.append("/user_login");

MultivaluedMap<String, String> body = new MultivaluedMapImpl();
body.add("username=", email);
body.add("password=", password);

System.out.println("login url: " + sb.toString());

WebResource webResource = Client.create(new DefaultClientConfig()).resource(sb.toString());

WebResource.Builder builder = webResource.accept("*");
builder.type("application/x-www-form-urlencoded");

ClientResponse response = builder.post(ClientResponse.class, body);

if (response.getStatus() != 200) {
throw new RuntimeException("failed: http error code " + response.getStatus());
}
System.out.println("Response from server: " + response.getEntity(String.class));

} catch (Exception e) {
e.printStackTrace();
}
}
}

完整的普通java登录功能:

public String postUserLogin(String username, String password) {
String result = "";
// URL for API to login
String url = "https://wifiplugapi.co.uk:3081/zcloud/api/user_login";
String requestParams = "username=" + username + "&password=" + password;

try {
URL obj = new URL(url);
System.out.println("login url: " + obj);

// Opens the connection
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

// Send POST request
con.setDoOutput(true);
con.setDoInput(true);

// Request Headers
con.setRequestMethod("POST");

// Sets all the headers
SetDefaultHeaders(con);

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
wr.write(requestParams);// adds values to the request
wr.flush();
wr.close();

// Handles the response
StringBuilder sb = new StringBuilder();
int responseCode = con.getResponseCode();
if (responseCode == 200) {
// if the request was successful OK = 200
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
// Returns Token
} else {
// If the request was bad, reason will be printed
result = "Error, login request failed";
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());
}

result = sb.toString();

// JSON Parser
JsonParser parser = new JsonParser();

JsonObject resultObj = parser.parse(result).getAsJsonObject();
con.disconnect();

if (resultObj.get("token") != null) {
result = (resultObj.get("token")).toString();
System.out.println("JSONObject Result (token): " + result);
} else {
System.out.println("result = " + result);
}
} catch (Exception e) {
e.printStackTrace();
}

// returns token value in string ie. fdg573gb3789gv923378gy83g3
result = result.replaceAll("\"", "");
return result;
}

最佳答案

执行 body.add 时,键中不应包含 =。将为您添加

MultivaluedMap<String, String> body = new MultivaluedMapImpl();
body.add("username=", email); // remove the =
body.add("password=", password); // remove the =

关于Java Jersey-客户端 header 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com