我试图做POST与HttpURLConnection(我需要使用这种方式,不能使用HttpPost),我想添加参数的连接,如
post.setEntity(new UrlEncodedFormEntity(nvp));
在哪里
nvp = new ArrayList<NameValuePair>();
有一些数据存储在。我找不到一种方法如何添加这个数组列表到我的HttpURLConnection在这里:
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
出现这种尴尬的https和http组合的原因是不需要验证证书。不过,这不是问题,它可以很好地发布服务器。但我需要它来张贴论点。
什么好主意吗?
重复的免责声明:
早在2012年,我还不知道如何在HTTP POST请求中插入参数。我一直在使用NameValuePair,因为它在教程中。这个问题可能看起来像一个重复的问题,然而,我2012年自己读了另一个问题,它没有使用NameValuePair。事实上,这并没有解决我的问题。
一个解决方案是让你自己的params字符串。
这是我在我的最新项目中使用的实际方法。你需要将args从hashtable更改为namevaluepair:
private static String getPostParamString(Hashtable<String, String> params) {
if(params.size() == 0)
return "";
StringBuffer buf = new StringBuffer();
Enumeration<String> keys = params.keys();
while(keys.hasMoreElements()) {
buf.append(buf.length() == 0 ? "" : "&");
String key = keys.nextElement();
buf.append(key).append("=").append(params.get(key));
}
return buf.toString();
}
发布参数:
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(getPostParamString(req.getPostParams()));
通过使用org.apache.http.client.HttpClient,你也可以用下面更容易读懂的方式轻松做到这一点。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
在try catch内可以插入
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
参数到HttpURLConnection使用POST使用NameValuePair输出
try {
URL url = new URL("https://yourUrl.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject data = new JSONObject();
data.put("key1", "value1");
data.put("key2", "value2");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
试试这个:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("your url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("user_name", "Name"));
nameValuePairs.add(new BasicNameValuePair("pass","Password" ));
nameValuePairs.add(new BasicNameValuePair("user_email","email" ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String ret = EntityUtils.toString(response.getEntity());
Log.v("Util response", ret);
您可以根据需要添加任意数量的nameValuePairs。别忘了在名单上提到人数。