0

I'm creating a java desktop application in netbeans for internal use, this is my first java app.

I created a login screen and trying to post user name and password to local wamp server.

I used following http utility class to send post data to server: An HTTP utility class to send GET/POST request

And this is my code to call send Post Request

    final String url = "http://localhost/timesheet/api/login";
    final String userName = loginName.getText();
    final String passwd = pwd.getText();
    if (null == userName || userName.equals("")) {
        lbl = "Please enter your login name";
    } else if (null == passwd || passwd.equals("")) {
        lbl = "Please enter your password";
    } else {
        Map<String, String> data = new HashMap<String, String>();
        data.put("user_name", userName.trim());
        data.put("pwd", passwd.trim());
        String requestURL = "http://localhost/timesheet/api/login";
        try {
            HttpUtility.sendPostRequest(requestURL, data);
            String[] response = HttpUtility.readMultipleLinesRespone();
            for (String line : response) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        HttpUtility.disconnect();

This code send request successfully and get responce if i do not pass any parameters or pass only one parameter(Either user_name or pwd) in request.

But when i send both parameter (user_name & pwd) it show 500 internal sever in responce?

I tried searching but not found useful. pleas help me to solve this.

The api code works with php, so don't think issue is with server. something happing in java code that cause 500 internal sever error.

Thanks.

Apache Access Log Entries

127.0.0.1 - - [03/Jan/2014:17:42:32 +0530] "POST /timesheet/api/login HTTP/1.1" 500 1371
127.0.0.1 - - [03/Jan/2014:17:44:07 +0530] "POST /timesheet/api/login HTTP/1.1" 500 1371

2 Answers 2

1

The code you are using contains at least one bug. It sends an extra & at the end of the request. I am not sure if this is what is causing the problem.

I recommend you do not use code snippets from random websites that you do not have any reason to trust. Today programming has become very popular and there are a lot of people who do not really know what they are doing posting code on the internet. Instead, I recommend you use Apache Commons httpclient or another such HTTP library.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot.. i used httpclient-4.3.1 of apache now same php code working. :)
0

You are getting 500 Response. It means your request is reaching at the server, but at the server some error is occurring.

You need to trace the request log in the server. Check the sent parameters matches the required ones

Also, check the request parameter name ( user_name or login_name)

2 Comments

127.0.0.1 - - [03/Jan/2014:17:42:32 +0530] "POST /timesheet/api/login HTTP/1.1" 500 1371 127.0.0.1 - - [03/Jan/2014:17:44:07 +0530] "POST /timesheet/api/login HTTP/1.1" 500 1371
have you tested the server with any other tool. The issue is at server while processing the request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.