简体   繁体   English

在libGDX中发送POST请求

[英]Sending a POST request in libGDX

How would I go about sending a POST request using libGDX in order to act as if I was logging in? 我该如何使用libGDX发送POST请求,以便像登录时一样工作?

When I use Postman to send a POST request to my server using IP:8080/login?email=EMAIL&password=12345. 当我使用Postman使用IP:8080 / login?email = EMAIL&password = 12345将POST请求发送到服务器时。 It shows the HTML code for the correct page after you login. 登录后,它将显示正确页面的HTML代码。 However when using the below code in libGDX it gives me the main login page. 但是,当在libGDX中使用以下代码时,它将为我提供主登录页面。 I've tried using setContent with and without the ?. 我尝试使用带有和不带有?的setContent。 I've tried using a Map that contains email and password and passing that into the setContent with HttpParametersUtils.convertHttpParameters(), but no luck. 我尝试使用包含电子邮件和密码的Map,并使用HttpParametersUtils.convertHttpParameters()将其传递到setContent中,但是没有运气。

I also read a post in regards to sending a POST request with Java that requires you to open the connection first before passing in the parameters. 我还阅读了有关使用Java发送POST请求的文章,该文章要求您在传入参数之前先打开连接。 But if this the case I have no idea how to go about it using libGDXs networking methods. 但是,在这种情况下,我不知道如何使用libGDXs网络方法进行操作。

This is currently what I have. 这是我目前所拥有的。

 String URL = "http://IP:8080/login";
    Net.HttpRequest httpPOST = new Net.HttpRequest(Net.HttpMethods.POST);
    httpPOST.setUrl(URL);
    httpPOST.setContent("?email=EMAIL&password=12345");

    Gdx.net.sendHttpRequest(httpPOST, new Net.HttpResponseListener() {
                @Override
                public void handleHttpResponse(Net.HttpResponse httpResponse) {
                    Gdx.app.log("MSG", httpResponse.getResultAsString());
                }

                @Override
                public void failed(Throwable t) {
                    Gdx.app.log("LOGIN", "was NOT successful!");
                }

                @Override
                public void cancelled() {
                    Gdx.app.log("LOGIN", "was cancelled!");
                }
    });

Your content is built like you are sending a GET request and not a POST. 构建内容的方式就像发送GET请求而不是POST。 The simplest way for you is simply changing Net.HttpMethods.POST to Net.HttpMethods.GET . 最简单的方法就是将Net.HttpMethods.POST更改为Net.HttpMethods.GET You are then still sending data to the server, but in the URL. 然后,您仍然在URL中向服务器发送数据。 if you want to send it via the http body you have to use POST. 如果要通过http正文发送,则必须使用POST。 But then you have to remove the GET syntax by removing the questionmark ("?"). 但是随后您必须通过删除问号(“?”)来删除GET语法。

A good resource for the difference between POST and GET is: https://www.w3schools.com/tags/ref_httpmethods.asp POST和GET之间的区别的一个好资源是: https : //www.w3schools.com/tags/ref_httpmethods.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM