HttpURLConnection类,如何通过邮递方式寄往参数? [英] HttpURLConnection, how to sending parameters via post?

查看:149
本文介绍了HttpURLConnection类,如何通过邮递方式寄往参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    String pathToOurFile = "/sdcard/DCIM/Camera/foto.jpg";
    String urlServer = "http://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:
<一href="http://blog.rafaelsanches.com/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/" rel="nofollow">http://blog.rafaelsanches.com/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/

I found this blog useful for writing some multipart form data code:
http://blog.rafaelsanches.com/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类,如何通过邮递方式寄往参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆