时人不识凌云木,直待凌云始道高。这篇文章主要讲述Java读取数据POST内容类型application / x-www-form-urlencoded相关的知识,希望能为你提供帮助。
我有一个我正在测试的API。 API接收POST请求并像这样读取它
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
System.out.println("jb: "+jb);
System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
} catch (Exception e) { /*report an error*/ }
当我在“application / json; charset = utf-8”中发送POST请求时,一切正常
httpPost.setHeader("content-type", "application/json;
charset=utf-8");
它打印这个:
jb: {"client_domain":"=....);
//proper Json data
request.getHeader('content-type'): application/json;
charset=utf-8
我可以正确地读取数据。
但是我的问题是当我以相同的方式发送数据但我设置了内容类型“application / x-www-form-urlencoded; charset = utf-8”
httpPost.setHeader("content-type", "application/x-www-form-urlencoded;
charset=utf-8");
测试是相同的只是内容类型不同但似乎我不再收到任何数据:
jb:
request.getHeader('content-type'): application/x-www-form-urlencoded;
charset=utf-8
任何的想法?
///更新
这是Spring Controller
@RequestMapping(value = https://www.songbingjia.com/android/{"user/add"}, method = RequestMethod.POST, produces="application/json;
charset=utf-8")
@ResponseBody
public ResponseEntity<
String>
getNewUserApi(HttpServletRequest request,
HttpServletResponse response) throws Exception {Map<
String, Object>
jsonObj = new HashMap<
String, Object>
();
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
System.out.println("jb: "+jb);
System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
} catch (Exception e) { /*report an error*/ }
///I create my JSon that will be sent back
return JsonUtils.createJson(jsonObj);
//更新2以下是我发送数据的方式
public static void main(String[] args) throws Exception {String url = "http://localhost:8080/child/apiv1/user/add";
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("content-type", "application/x-www-form-urlencoded;
charset=utf-8");
try {
//we had to the parameters to the post request
JSONObject json = new JSONObject();
json.put("client_id", "fashfksajfhjsakfaskljhflakj");
json.put("client_secret", "9435798243750923470925709348509275092");
json.put("client_domain", "dummy.localhost.com");
//create the user json object
JSONObject userObj = new JSONObject();
userObj.put("email", "johnsmith42@yopmail.com");
userObj.put("name", "Anna Sax");
JSONArray childrenArray = new JSONArray();
JSONObject child1 = new JSONObject();
child1.put("name", "Iphone 6");
child1.put("age", "2");
childrenArray.put(child1);
userObj.put("children", childrenArray);
json.put("user", childObj);
StringEntity params = new StringEntity(json.toString());
httpPost.setEntity(params);
System.out.println("executing request: " + httpPost.getRequestLine());
HttpResponse response;
response = client.execute(httpPost);
//[...]} //End main
我知道创建一个Json并将其发送到“application / x-www-form-urlencoded”并没有多大意义,但只是我们的一个用户无法解决他的问题,它只会发送“application /的X WWW窗体-urlencoded”。
答案【Java读取数据POST内容类型application / x-www-form-urlencoded】@RequestMapping的'produce'属性显示客户端只能接受application / json数据,因此您可以删除它或将其更改为'application / x-www-form-urlencoded'。
推荐阅读
- solr exception预期的mime类型application / octet-stream但是得到了text / html
- 如何在Android MVVM架构中实现对话框选择选项
- Zappa部署错误(GET请求产生502响应代码)
- QQuickWindow在共享lib自动关闭时在QApplication中显示
- UIActivityIndi cator添加AppDelegate
- 如何使用swift从Xcode coco app运行像FFMPEG这样的终端程序()
- 具有本地化app.staticTexts的UI测试(XCTest)
- 编译Android Studio Vulkan示例
- 引用oracle apex APP_USER