怎样获得HttpEntity里面的内容解决方案
展开全部
通过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 这个类里面有方法
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
ZESTRON
2024-09-04 广告
2024-09-04 广告
表面污染分析包括评估表面上存在的颗粒、残留物或物质。通过利用显微镜、光谱学和色谱法等技术,分析人员可以识别和表征污染物,以确定其成分和来源。这种分析在电子、制药和制造等各个行业中至关重要,以确保产品质量、性能和安全性。了解表面污染有助于实施...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询