【发布时间】:2015-03-08 19:17:13
【问题描述】:
我正在尝试在 Android 应用程序中发送 HTTP Post 请求。
这是我应该发送的:
这是我应该收到的:
所以,总而言之,我需要从响应中获取 cookie,响应代码应该是 302。
我有以下 Android 代码:
private static String makePostRequest() {
HttpClient httpClient = new DefaultHttpClient();
// replace with your url
HttpPost httpPost = new HttpPost("http://g1.botva.ru/login.php");
boolean flag = httpClient.getParams().isParameterTrue(ClientPNames.HANDLE_REDIRECTS);
httpPost.addHeader("Accept", "*/*");
httpPost.addHeader("Accept-Encoding", "gzip, deflate");
//httpPost.addHeader("Content-Length", "83");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.addHeader("User-Agent", "runscope/0.1");
Header [] arr = httpPost.getAllHeaders();
String result123 = "";
//Post Data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
nameValuePair.add(new BasicNameValuePair("do_cmd", "login"));
nameValuePair.add(new BasicNameValuePair("server", "1"));
nameValuePair.add(new BasicNameValuePair("email", "avmalyutin@mail.ru"));
nameValuePair.add(new BasicNameValuePair("password", "avmalyutin1234"));
nameValuePair.add(new BasicNameValuePair("remember", "1"));
//Encoding POST data
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace();
}
//making POST request.
try {
HttpResponse response = httpClient.execute(httpPost);
String getCookie = response.getHeaders("Pragma").length + "";
result123 = response.getStatusLine().getStatusCode()+"";
// write response to log
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
return result123;
}
我收到代码 200。Cookies 中只有 PHPSESSIONID,但没有其他 cookie。
【问题讨论】:
-
嘿 CodeMonkey,因为你已经在使用Runscope——你可能想查看documentation around Android。多个不同库的大量示例,以及有关如何通过您自己的捕获 URL 传递请求的提示,以便您可以在 Runscope Traffic Inspector 中检查 Android 客户端进行的 API 调用。
标签: android post cookies httprequest