0

How to build the URL for HTTP POST request?

4

2 回答 2

1

你确定你在发帖吗?上面的 url 看起来像 GET。

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>405 HTTP method GET is not supported by this URL</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: HTTP method GET is not supported by this URL</h1>
</body></html>
于 2013-08-30T08:46:44.713 回答
1

根据API 文档,您应该将 JSON 数据作为POST方法的请求正文而不是 URL 参数传递。

所以它应该看起来像这样:

String data = "{\"data\": [{\"text\": \"I love Titanic.\"}, {\"text\": \"I hate Titanic.\"}]}";

URL url = new URL("http://www.sentiment140.com/api/bulkClassifyJson?appid=me@gmail.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");

// write the request body
connection.getOutputStream().write(data.getBytes("UTF8"));

// get the response and read it
InputStream in = connection.getInputStream();
于 2013-08-30T08:50:48.437 回答