Android:通过Http POST发送byte []数组

Oce*_*lue 11 android http-post

我能够对参数字符串进行POST.我使用以下代码:

String parameters = "firstname=john&lastname=doe";
URL url = new URL("http://www.mywebsite.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(parameters);
out.flush();
out.close();
connection.disconnect();
Run Code Online (Sandbox Code Playgroud)

但是,我需要对二进制数据进行POST(以byte []的形式).

不知道如何更改上面的代码来实现它.
有人可以帮我这个吗?

小智 9

看一下这里 在Android中发送POST数据

但是使用ByteArrayEntity.

byte[] content = ...
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(content));           
HttpResponse response = httpClient.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)


kab*_*uko 6

您可以首先对数据进行base-64编码.看一下恰当命名的Base64类.