我正在尝试使用httpClient(通过apache)发布和获取数据。发布是绝对正确的,我的代码没有问题,但是,我不能说获取数据相同。
我尝试从中获取数据的网站是:http://www.posttestserver.com/data/2013/04/16/01.13.04594755373
我只想接收帖子的主体(即从最近的情况开始的底部的JSON字符串),但是,我当前使用的方法(以及我在网上找到的每个方法)都返回时间,源IP,标题和正文(基本上它返回所有内容)。无论如何要解析这个的身体?我不想通过返回的字符串并告诉它查找文本“Begin Post Body”,我想要一个自然的方法来做到这一点。那存在吗?
TLDR:我只希望它返回帖子正文中的内容
这是我的代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public static void main(String[] args) throws ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.posttestserver.com/data/2013/04/16/01.41.38521171013");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
}
这是返回的内容:
Time: Tue, 16 Apr 13 01:41:38 -0700
Source ip: 155.198.108.247
Headers (Some may be inserted by server)
UNIQUE_ID = UW0OwtBx6hIAACfjfl4AAAAA
CONTENT_LENGTH = 7627
CONTENT_TYPE = application/json
HTTP_HOST = posttestserver.com
HTTP_CONNECTION = close
HTTP_USER_AGENT = Apache-HttpClient/4.2.4 (java 1.5)
REMOTE_ADDR = 155.198.108.247
REMOTE_PORT = 54779
GATEWAY_INTERFACE = CGI/1.1
REQUEST_METHOD = POST
QUERY_STRING =
REQUEST_URI = /post.php
REQUEST_TIME = 1366101698
No Post Params.
== Begin post body ==
{"Recent Cases":[{"descript..etc etc"}]}
== End post body ==
有什么想法吗?
答案 0 :(得分:0)
您可以将以下方法发送给网址,它会在字符串中为您提供响应而不包含任何标题详细信息,因此在您的示例中只是json。
private static String readUrl(final String urlString) throws Exception {
BufferedReader reader = null;
try {
final URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
final StringBuffer buffer = new StringBuffer();
int read;
final char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
return buffer.toString();
} finally {
if (reader != null) {
reader.close();
}
}
}