[英]Extracting the post body from an HTTP response
I am trying to use httpClient (by apache) to post and get data. 我正在尝试使用httpClient(通过apache)发布和获取数据。 Posting is absolutely fine and my code for that has no issues, however, I cannot say the same for getting data.
发布是绝对可以的,我的代码没有问题,但是,对于获取数据我不能说同样的话。
The website I am trying to get data from is this: http://www.posttestserver.com/data/2013/04/16/01.13.04594755373 我试图从中获取数据的网站是: http : //www.posttestserver.com/data/2013/04/16/01.13.04594755373
I only want to receive the body of the post (ie the JSON string at the bottom starting with Recent Cases), however, the method I am currently using (and every method I find online) returns the time, source IP, headers and body (basically it returns everything). 我只想接收帖子的正文(即,以“最近的案例”开头的底部的JSON字符串),但是,我当前正在使用的方法(以及我在网上找到的每个方法)都会返回时间,源IP,标头和正文(基本上它会返回所有内容)。 Is there anyway to parse the body of out this?
无论如何,要对此进行解析吗? I don't want to go through the returned string and tell it to look for the text "Begin Post Body", I want a natural method for doing this.
我不想遍历返回的字符串并告诉它查找文本“ Begin Post Body”,我想要一个自然的方法。 Does that exist?
是否存在?
TLDR: I only want it to return what's in the post body TLDR:我只希望它返回帖子正文中的内容
Here's my code: 这是我的代码:
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));
}
And here's what is returned: 这是返回的内容:
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 ==
Any ideas? 有任何想法吗?
You can send the following method a url and it will give you the response in a String without any header details, so in your example just the json. 您可以向以下方法发送一个url,它将以String形式给您响应,而没有任何标头详细信息,因此在您的示例中仅为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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.