如何构建HTTP POST请求的URL?

时间:2013-08-30 08:33:20

标签: java

如何构建HTTP POST请求的URL?

2 个答案:

答案 0 :(得分:1)

你确定要发帖吗?以上网址看起来像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>

答案 1 :(得分:1)

根据API documentation,您应该将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();