在 Java 中使用来自 POST 的特定数据
Using Specific Data from POST in Java
使用 POST 到 www.httpbin.org/post(测试 POST 函数)并输入 "Hello World!",我得到以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {
"Hello World!": ""
},
"headers": {
"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Content-Length": "12",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"
},
"json": null,
"origin": "98.164.222.196",
"url": "http://httpbin.org/post"
}
Resp Code:200
Resp Message:OK
基于此,我知道 POST 正在运行。现在我只想从 form
变量中获取输入并获得 Hello World!
的字符串。
我正在尝试使用 JSON 来解析它,但是 运行 遇到了问题...从 [=14] 获取 Hello World!
的方法是什么=]变量?
POST代码:
String httpURL = "http://httpbin.org/post";
String query = textInput;
URL myurl = new URL(httpURL);
HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
{
response += (char)c;
}
input.close();
System.out.println("" + response);
//JSONObject json = new JSONObject(response);
//String uname = json.getJSONObject("form").getString("data");
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
getString("data")
给我带来了问题...我注意到如果我通过 POST 方法发送文本 "data",程序似乎工作正常,或者否则它会崩溃并显示 JSON 对象不存在的错误。
问题是您在请求中提供的 Header "Content-Type": "application/x-www-form-urlencoded" 错误。您声明您发送表单数据,但是您在 Body 中发送纯文本。服务器正在尝试解析您的请求,并为您提供解析后的信息。
如果您提供正确的 Mime-Type "Content-Type":"text/plain",您会得到如下内容:
{
"args": {}
"data": "Hello world!"
"files": {}
"form": {}
"headers": { ....
然后你可以通过你想使用的代码得到你想要的:getString("data")
使用 POST 到 www.httpbin.org/post(测试 POST 函数)并输入 "Hello World!",我得到以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {
"Hello World!": ""
},
"headers": {
"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Content-Length": "12",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"
},
"json": null,
"origin": "98.164.222.196",
"url": "http://httpbin.org/post"
}
Resp Code:200
Resp Message:OK
基于此,我知道 POST 正在运行。现在我只想从 form
变量中获取输入并获得 Hello World!
的字符串。
我正在尝试使用 JSON 来解析它,但是 运行 遇到了问题...从 [=14] 获取 Hello World!
的方法是什么=]变量?
POST代码:
String httpURL = "http://httpbin.org/post";
String query = textInput;
URL myurl = new URL(httpURL);
HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
{
response += (char)c;
}
input.close();
System.out.println("" + response);
//JSONObject json = new JSONObject(response);
//String uname = json.getJSONObject("form").getString("data");
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
getString("data")
给我带来了问题...我注意到如果我通过 POST 方法发送文本 "data",程序似乎工作正常,或者否则它会崩溃并显示 JSON 对象不存在的错误。
问题是您在请求中提供的 Header "Content-Type": "application/x-www-form-urlencoded" 错误。您声明您发送表单数据,但是您在 Body 中发送纯文本。服务器正在尝试解析您的请求,并为您提供解析后的信息。
如果您提供正确的 Mime-Type "Content-Type":"text/plain",您会得到如下内容:
{
"args": {}
"data": "Hello world!"
"files": {}
"form": {}
"headers": { ....
然后你可以通过你想使用的代码得到你想要的:getString("data")