HttpURLConnection类,如何通过邮递方式寄往参数?

编程入门 行业动态 更新时间:2024-10-25 20:30:26
本文介绍了HttpURLConnection类,如何通过邮递方式寄往参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 String pathToOurFile = "/sdcard/DCIM/Camera/foto.jpg"; String urlServer = "server/upload.php"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; . . . URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); outputStream = new DataOutputStream( connection.getOutputStream() ); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd);

与code以上,我上传图像到服务器上,而不是如何通过邮寄传递参数字符串类型

with the code above, i upload a image to server, but not how to pass parameters string type via post

有谁知道?

推荐答案

我发现这个博客有用写一些多部分的表单数据code: blog.rafaelsanches/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/

I found this blog useful for writing some multipart form data code: blog.rafaelsanches/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/

不要忘记更改边界变量来匹配您在的HttpURLConnection 你是指定哪些使用以多部分形式发送到服务器。

Don't forget to change the boundary variable to match what you specify in the HttpURLConnection that you're using to send the multipart form to the server.

更多推荐

HttpURLConnection类,如何通过邮递方式寄往参数?

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

发布评论

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

>www.elefans.com

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