gpt4 book ai didi

android - 通过 httpurlconnection 更改 HttpPost

转载 作者:行者123 更新时间:2023-11-30 00:25:44 27 4
gpt4 key购买 nike

我有一种方法可以将一些数据发送到服务器并获得 cookie 响应。这是我的方法:

 public Cookie getCookie(String username, String password) {

try {
JSONObject obj = new JSONObject();
key = UUID.randomUUID().toString();
userEncrypt = .....
passwordEncrypt = ......
obj.put("username", userEncrypt);
obj.put("password", passwordEncrypt);
obj.put("customCredential", key + ",Mobile" + deviceId);
obj.put("isPersistent", false);

HttpPost request = new HttpPost(AppUtil.getConfig(baseActivity, App.SERVICE_AUTH) + "Login");
request.setHeader("Content-Type", "application/json; charset=utf-8");
request.setEntity(new StringEntity(obj.toString(), "utf-8"));
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);

String result = EntityUtils.toString(response.getEntity(), "UTF-8");
if (result.toLowerCase(Locale.US).equals("true")) {

for (Cookie cookie : client.getCookieStore().getCookies()) {
if (cookie.getName().contains(".ASPXAUTH"))
return cookie;
}
}

我想使用 httpurlconnection 但我不知道如何更改它?如何通过 httpurlconnection 返回 cookie?

这是我写的新方法:

           String result = "";
HttpURLConnection connection = null;
connection = (HttpURLConnection) new URL(AppUtil.getConfig(this, App.SERVICE_AUTH) + "Login").openConnection();

connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");

OutputStream os = connection.getOutputStream();
os.write((obj.toString()).getBytes("UTF-8"));
os.flush();

int responseCode = connection.getResponseCode();

我在 responseCode 上得到了 400。有人可以帮忙吗?thnx

最佳答案

使用它,希望它有效。

 BufferedWriter  writer = new BufferedWriter(
new OutputStreamWriter(outputStream, "UTF-8"));

String s = "yourJsonName =" json.toString();
writer.write(s);
writer.flush();
writer.close();

要获取 cookie 并在下一个请求中再次发送它,您可以这样做:

SharedPreferences preferences = this.getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
if (!preferences.getString("cookieName", "no_cookie").equals("no_cookie")) {
connection.setRequestProperty("Cookie", preferences.getString("cookieName", "no_cookie"));
}

for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
SharedPreferences.Editor editor = preferences.edit();
if (headerName.equals("Set-Cookie")) {
String cookie = connection.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
editor.putString("cookieName", cookie);
editor.apply();
break;
}
editor.putString("cookieName", "no_cookie");
}

connection.connect();

为了从 inputStream 中获取 json,我强烈建议你像下面这样使用 Gson:

private ArrayList<YourClassOfObject> readJson(InputStream is) throws IOException {

ArrayList<YourClassOfObject> c = new ArrayList<>();
try{

Reader reader = new InputStreamReader(is , "UTF-8");
Gson gson = new Gson();
YourClassOfObject[] p;
p = gson.fromJson(reader, YourClassOfObject[].class);
c.addAll(Arrays.asList(p));

}catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}catch (ExceptionInInitializerError e){
e.printStackTrace();
}
return c;
}

关于android - 通过 httpurlconnection 更改 HttpPost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45415399/

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