我有一个服务器和一个客户端。我的服务器以文本格式发送HttpResponse。我可以在客户端接收到文本。我需要将文本转换回HttpResponse对象。有没有什么开源库或者内置的java/Android机制可以做到这一点呢?
我所能做的就是把HttpResponse转换成文本。
发布于 2014-11-04 16:34:52
试试这条路
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.getEntity().writeTo(baos);
byte[] bytes = baos.getBytes();然后,您可以将内容添加到另一个HttpResponse对象,如下所示:
HttpResponse response = httpClient.execute(new HttpGet(URL));
response.setEntity(new ByteArrayEntity(bytes));发布于 2014-11-04 18:41:38
只需使用Apache Commons IO:http://commons.apache.org/proper/commons-io/即可完成此操作
private byte[] receiveByteArrayData(HttpURLConnection httpURLConnection)
throws IOException
{
return IOUtils.toByteArray(httpURLConnection.getInputStream());
}发送byte[]数据:
URL url = new URL("http://your.url.here:yourPortNumber/something");
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestProperty("Content-Type", "application/octet-stream");
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setFixedLengthStreamingMode(bytes.length);
httpUrlConnection.connect();
httpUrlConnection.getOutputStream().write(bytes);
httpUrlConnection.getOutputStream().flush();
if (httpsUrlConnection.getResponseCode() == 200) //OK
{
System.out.println("successfully uploaded data");
}
httpUrlConnection.disconnect();发布于 2014-11-04 19:35:41
我从parse http response bytes in java那里得到了我想要的
感谢大家的回复:)
https://stackoverflow.com/questions/26730666
复制相似问题