如何使用 NameValuePair 使用 POST 向 HttpURLConnection 添加参数

编程入门 行业动态 更新时间:2024-10-25 12:20:38
本文介绍了如何使用 NameValuePair 使用 POST 向 HttpURLConnection 添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 HttpURLConnection 执行 POST(我需要以这种方式使用它,不能使用 HttpPost)并且我'想向该连接添加参数,例如

I am trying to do POST with HttpURLConnection(I need to use it this way, can't use HttpPost) and I'd like to add parameters to that connection such as

post.setEntity(new UrlEncodedFormEntity(nvp));

哪里

nvp = new ArrayList<NameValuePair>();

存储了一些数据.我找不到如何将此 ArrayList 添加到我的 HttpURLConnection 中的方法:

having some data stored in. I can't find a way how to add this ArrayList to my HttpURLConnection which is here:

HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); http = https; http.setRequestMethod("POST"); http.setDoInput(true); http.setDoOutput(true);

这种尴尬的 https 和 http 组合的原因是需要不验证证书.不过,这不是问题,它可以很好地发布服务器.但我需要它来张贴参数.

The reason for that awkward https and http combination is the need for not verifying the certificate. That is not a problem, though, it posts the server well. But I need it to post with arguments.

有什么想法吗?

重复免责声明:

回到 2012 年,我不知道如何将参数插入到 HTTP POST 请求中.我一直在使用 NameValuePair,因为它是在教程中.这个问题似乎是重复的,但是,我 2012 年的自我阅读 other 问题,它是 NOT 使用 NameValuePair.事实上,它并没有解决我的问题.

Back in 2012, I had no idea how parameters were inserted into an HTTP POST request. I was hanging on to NameValuePair because it was in a tutorial. This question might seem like a duplicate, however, my 2012 self read that other question and it was NOT using NameValuePair. It did not, in fact, solve my problem.

推荐答案

您可以获取连接的输出流并将参数查询字符串写入其中.

You can get output stream for the connection and write the parameter query string to it.

URL url = new URL("yoururl"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("firstParam", paramValue1)); params.add(new BasicNameValuePair("secondParam", paramValue2)); params.add(new BasicNameValuePair("thirdParam", paramValue3)); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getQuery(params)); writer.flush(); writer.close(); os.close(); conn.connect();

...

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for (NameValuePair pair : params) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(pair.getName(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); } return result.toString(); }

更多推荐

如何使用 NameValuePair 使用 POST 向 HttpURLConnection 添加参数

本文发布于:2023-10-12 00:12:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   参数   POST   NameValuePair   HttpURLConnection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!