Java发送登录请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6544339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:38:01  来源:igfitidea点击:

Java send post request for login

javahttppostlogin

提问by Bella

i want to login to application from java code.

我想从 java 代码登录到应用程序。

I have found the following code example:

我找到了以下代码示例:

 String requestURL = "http://applicationURL/login.jsp"; 
 String data = "[email protected]&password=123password&login=Login";


 public static String sendPostRequest(String data, String requestURL) {

         String result="";
        try {

            // Send the request
            URL url = new URL(requestURL);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

            //write parameters
            writer.write(data);
            writer.flush();

            // Get the response
            StringBuffer answer = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                answer.append(line);
            }
            writer.close();
            reader.close();

            //Output the response
            System.out.println(answer.toString());
            result = answer.toString();

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
         return result;
    }

but i can not login, it returns back only login page.

但我无法登录,它只返回登录页面。

If anybody can, please help me to understand what am i doing wrong.

如果有人可以,请帮助我了解我做错了什么。

I have added method and content-type, but it still does not work:

我添加了方法和内容类型,但它仍然不起作用:

conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

回答by duedl0r

you didn't specify which method you're using (GET, POST, ..). have a look here: sending http post request in java

您没有指定您使用的方法(GET、POST、..)。看看这里:在java中发送http post请求



Maybe you also have to set your Content-Type:

也许您还必须设置 Content-Type:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");


Edit: Try to analyze the HTTP stuff using a browser with a socket sniffer (e.g. wireshark). There might be some special cases like, cookies or server side verification stuff you missed. Probably some hidden field which is being sent or something like that.. (it is a html form, right?)

编辑:尝试使用带有套接字嗅探器(例如,wireshark)的浏览器分析 HTTP 内容。可能有一些特殊情况,例如您错过的 cookie 或服务器端验证内容。可能是一些正在发送的隐藏字段或类似的东西..(它是一个 html 表单,对吧?)

回答by Maurice Perry

The default method for an HttpURLConnection is GET. You need to change it to POST:

HttpURLConnection 的默认方法是 GET。您需要将其更改为 POST:

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod(HttpConnection.POST);

回答by Penkov Vladimir

check commons-httpclient

检查 commons-httpclient

this is example of post: http://hc.apache.org/httpclient-3.x/methods/post.html

这是帖子的例子:http: //hc.apache.org/httpclient-3.x/methods/post.html

回答by MariuszS

I prefer simple solution with JSoup:

我更喜欢使用 JSoup 的简单解决方案:

Document document  = Jsoup.connect("http://applicationURL/login.jsp")
        .data("email", "[email protected]")
        .data("password", "123password")
        .data("login", "login")
        .post();