0

我正在写我的http post header。

POST /\r\n
Content-Length=...\r\n
\r\n\r\n
file1=(bytearray data)&file2=(bytearray data)

我不确定如何将bytearray数据放在那里

4

1 回答 1

3

如果你想手动完成,你只需要正确编码你的参数:

byte[] array1 = new byte[10];
byte[] array2 = new byte[10];

StringBuilder builder = new StringBuilder();

builder.append("POST /\r\n");
builder.append("Content-Length=...\r\n");
builder.append("\r\n\r\n");
builder.append("file1=");
builder.append(URLEncoder.encode(new String(array1),"UTF-8"));
builder.append("&file2=");
builder.append(URLEncoder.encode(new String(array2),"UTF-8"));

或者您可以使用更清晰或更标准的东西HttpURLConnection

byte[] array1 = new byte[10];
byte[] array2 = new byte[10];

StringBuilder parameters = new StringBuilder();

parameters.append("file1=");
parameters.append(URLEncoder.encode(new String(array1),"UTF-8"));
parameters.append("&file2=");
parameters.append(URLEncoder.encode(new String(array2),"UTF-8"));

String request = "http://domain.com";
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset","UTF-8");
connection.setRequestProperty("Content-Length",Integer.toString(parameters.toString().getBytes().length));

DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(parameters.toString());
wr.flush();
wr.close();
connection.disconnect();

更多信息 :

于 2013-06-19T01:27:11.060 回答