Java – HTML publish request (login form)

I need to log in to a website I try to archive it by sending the correct post request I can collect the required data (two security tokens and a cookie), which seems to have no problem But the final login process didn't work - unfortunately: I didn't know where to find the problem, because the server just redirected me to the login page without any prompt

URL url = new URL("SERVER");
Map<String,Object> params = new LinkedHashMap<>();
params.put("security_token",security_token);
params.put("login_ticket",login_ticket);
params.put("loginname","USERNAME");
params.put("password","PASSWORD");
params.put("login","Login");

StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
    if (postData.length() != 0) postData.append('&');
    postData.append(URLEncoder.encode(param.getKey(),"UTF-8"));
    postData.append('=');
    postData.append(URLEncoder.encode(String.valueOf(param.getValue()),"UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();


conn.setRequestProperty("Cookie",cookie);


conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);

Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));

Map<String,List<String>> headerFields = conn.getHeaderFields();

I have tried to use c# to solve this problem The following code works properly, so I'm just trying to "translate" it into Java

string cookie = "COOKIEVALUE";
request.Host = new Uri("SERVER");
//request.PostData.Add("Cookie",cookies[0]);
request.PostData.Add("loginname","USERNAME");
request.PostData.Add("password","PW");
request.PostData.Add("login_ticket","269ba20ad5a6f1a0219a3a333d3a5997");
request.PostData.Add("security_token","ZHfyszNSuMrN8Xw80Pudka+5cZB20j+or+JWXCWzVPg=");

request.ContentType = "application/x-www-form-urlencoded";
try
{
    request.SendRequest(cookie);
    using (var response = request.GetResponse())
    {
        if (response == null)
            throw new WebException();
        using (var stream = response.GetResponseStream())
        {
            using (var streamReader = new StreamReader(stream))
            {
                string result = streamReader.ReadToEnd();

            }
        }
    }
}
catch (WebException)
{
    throw;
}

And sendrequest methods

public void SendRequest(string cookieValue,byte[] buffer = null)
{
    _webRequest = HttpWebRequest.CreateHttp(Host);
    _webRequest.Method = "POST";
    _webRequest.ContentType = ContentType;

    _webRequest.CookieContainer = new CookieContainer();
    var cookie = new Cookie("Seminar_Session",cookieValue);
    cookie.Domain = Host.Host;
    _webRequest.CookieContainer.Add(cookie);

    string postString = "";
    foreach (var item in PostData)
    {
        postString += item.Key + "=" + item.Value + "&";
    }

    if (postString.Length > 0)
        postString = postString.Remove(postString.Length - 1,1);

    byte[] postBuffer = System.Text.Encoding.UTF8.GetBytes(postString);

    _webRequest.ContentLength = postBuffer.Length;
    if (buffer != null) _webRequest.ContentLength += buffer.Length;

    using (var requestStream = _webRequest.GetRequestStream())
    {
        requestStream.Write(postBuffer,postBuffer.Length);
        if (buffer != null) requestStream.Write(buffer,buffer.Length);
        requestStream.Flush();
    }
}

Now I'm trying to make java code work Is there anything I'm obviously wrong? Unfortunately, I can't continue to use c# code. Now I need a Java working solution So, how to use an invalid java version to solve this problem?

Editor: finally, I used Apache components to handle these requests

String security_token;
    String login_ticket;

    //setup configuration
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();

    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(_cookieStore);

    //send request to get login data


    Document source;
    security_token = "TOKEN";
    login_ticket = "TICKET";
    HttpPost httppost = new HttpPost("https://www.studip.uni-goettingen.de/");

    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("security_token",security_token));
    params.add(new BasicNameValuePair("login_ticket",login_ticket));
    params.add(new BasicNameValuePair("loginname",_credentials.getUserName()));
    params.add(new BasicNameValuePair("password",_credentials.getpassword()));
    httppost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

    //Execute the login post request and get the response.
    HttpResponse response = httpClient.execute(httppost,context);
    httpentity entity = response.getEntity();

Solution

They use some hash functions to generate these security tokens In most cases, these hashes are hashed in full form, including the fields Your problem may be that you are not using it, so the hash value will be different If you reload the page, you will see login_ Ticket changes for the same reason

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>