怎样获得HttpEntity里面的内容解决方案
1个回答
展开全部
通过HttpPut进行了PUT请求,获得了返回的HttpEntity,并且判断出HttpEntity不为空,但是HttpEntity里面的内容取不出来,代码如下:
HttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null){
InputStream instreams = httpEntity.getContent();
String str = ConvertStreamToString(instreams);
System.out.println("Response:" + "\n" + str);
}
ConvertStreamToString()函数如下:
// Convert stream to string
public static String ConvertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
System.out.println("Error=" + e.toString());
} finally {
try {
is.close();
} catch (IOException e) {
System.out.println("Error=" + e.toString());
}
}
return sb.toString();
}
结果只输出了:
Response:
后面就没有了。输出了Response:,说明httpEntity != null,但是读出的字符串又是空的,这是怎么回事呢?是不是ConvertStreamToString()这个函数有问题,还是httpEntity本来就没东西?
------解决方案--------------------
不知道 你什么原因应该可以的。
这个自带的http
String s= "http://xinjinqiao.tprtc.com/admin/main/pro!lrprolist.do";
URL url = new URL(s);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
http.connect();
OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
String input = "name=flr&nowpage=1&pagesize=10";
out.append(input);
out.flush();
out.close();
int length = (int) http.getContentLength();
System.out.println(length);
BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
http.disconnect();
System.out.println(buffer.toString());
------解决方案--------------------
EntityUtils 这个类里面有方法
HttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null){
InputStream instreams = httpEntity.getContent();
String str = ConvertStreamToString(instreams);
System.out.println("Response:" + "\n" + str);
}
ConvertStreamToString()函数如下:
// Convert stream to string
public static String ConvertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
System.out.println("Error=" + e.toString());
} finally {
try {
is.close();
} catch (IOException e) {
System.out.println("Error=" + e.toString());
}
}
return sb.toString();
}
结果只输出了:
Response:
后面就没有了。输出了Response:,说明httpEntity != null,但是读出的字符串又是空的,这是怎么回事呢?是不是ConvertStreamToString()这个函数有问题,还是httpEntity本来就没东西?
------解决方案--------------------
不知道 你什么原因应该可以的。
这个自带的http
String s= "http://xinjinqiao.tprtc.com/admin/main/pro!lrprolist.do";
URL url = new URL(s);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
http.connect();
OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
String input = "name=flr&nowpage=1&pagesize=10";
out.append(input);
out.flush();
out.close();
int length = (int) http.getContentLength();
System.out.println(length);
BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
http.disconnect();
System.out.println(buffer.toString());
------解决方案--------------------
EntityUtils 这个类里面有方法
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询