为了账号安全,请及时绑定邮箱和手机立即绑定

在 Java Servlet 中发送 JSON/GSON 请求会导致“错误请求”

在 Java Servlet 中发送 JSON/GSON 请求会导致“错误请求”

红颜莎娜 2022-04-28 17:08:16
我有想要获得有效响应的 servlet 代码。这是请求的原始布局 {  "function": "Check",  "teamId": "<teamId>",  "teamKey": "<teamKey>",  "requestId": "<request-id>",  "firstName": "<FirstName>",  "lastName": "<LastName>",  "ticketNumber": "<ticket-num>"}我在 Intellij 的 servlet 中有这个。import com.google.gson.*;import com.google.*;import org.apache.*;import org.apache.http.*;@WebServlet(name = "Logincheck", urlPatterns = {"/Logincheck"})public class Servlet extends HttpServlet {String teamID = "IC106-2";String teamKey = "1b3741ccf6d9ec5245055370125d901e";String url="http://fys.securidoc.nl:11111/Ticket";int Min = 1;int Max = 100;int REQ_ID =  Min + (int)(Math.random() *((Max - Min)+1));    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//index.html form user input     String fname = request.getParameter("firstName");    String lastName = request.getParameter("lname");    String ticketNummer = request.getParameter("ticketnr");    JsonParser parser = new JsonParser();    URL object=new URL(url);    String ticketCheck = "{\"function:\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";    }}现在,当我点击运行时,它会打开我的 index.html,当我单击登录按钮到 /Logincheck 时,它会响应(在 intellij 内):Bad RequestTeamkey 和 TeamID 100% 正确,但我可能忽略了与 Json 相关的一些事情。一般来说,我对 Json 和 servlet 的经验很少。就像我制作硬编码的登录和票证凭据一样,还是用户的输入正确已经正确?我应该期待这样的回应:{  "ticketStatus": "<ticket-status>",  "requestId": "<request-id>",  "result": "<result-code>",  "resultText": "<result-text>"}
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

您应该将 json 数据写入请求正文。为此,您可以使用OutputStreamWriter类写入如下输出流HttpURLConnection:


String ticketCheck = "{ \"function\":\"Check\",\"teamId\":IC106-2,\"teamKey\":1b3741ccf6d9ec5245055370125d901e,\"requestId\":1,\"firstName\":\"" + fname + "\" ,\"lastName\":\""+lastName+"\",\"ticketNumber\":\"\"" + ticketnummer + "\"}";


HttpURLConnection con = (HttpURLConnection) object.openConnection();

con.setDoOutput(true);

con.setDoInput(true);

con.setRequestProperty("Content-Type", "application/json");

con.setRequestProperty("Accept", "application/json");

con.setRequestMethod("POST");


OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

writer.write(ticketCheck);

//this line closes the outputstream and actually makes the http request is sent

writer.flush();


查看完整回答
反对 回复 2022-04-28
?
holdtom

TA贡献1805条经验 获得超10个赞

解决了。简单的报价错字。“错误请求”意味着语法错误,我忘记在函数旁边添加 \" 。

    String ticketCheck = "{\"function\":\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";

上面正确的json


查看完整回答
反对 回复 2022-04-28
  • 2 回答
  • 0 关注
  • 190 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信