Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6157347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:46:10+00:00 2026-05-23T20:46:10+00:00

In Java, if I want to send data to form on the server, where

  • 0

In Java, if I want to send data to form on the server, where form type is:

 <form method="post" id="form1" name="form1" action="">
 <div id="login" class="box">
  <div class="header">Log in</div>
  <div class="content">
   <label for="txtUser">User:</label>
   <input id="txtUser" name="txtUser" type="text" size="13" value="" />
   <label for="txtPassword">Password:</label>
   <input id="txtPassword" name="txtPassword" type="password" size="13" value="" />
   <input id="BLogin" name="BLogin" type="submit" value="Log in"  />
  </div>
  <div class="footer">
   <input type="checkbox" id="chkSave" name="chkSave"   /> <label for="chkSave">Save account</label>
  </div>
 </div>
 </form>

in this case I must use HttpPost method, since the form accepts the method “post” as it is stated in the form definition(initialization):

<form method="**post**" id="form1" name="form1" action="">

In my example(android solution) i am using the

__VIEWSTATE
__EVENTTARGET
__EVENTARGUMENT
ctl00$tbUsername
ctl00$tbPwd
ctl00$chkRememberLogin
ctl00$cmdLogin

values, since they are the once that are required by the server to make the post. Where do I find what is required by the server in the case when you have not programmed the server? I am using the WireShark software to see all the incomming responses or outgoing requests between the client and the server, just use the http filter to see only the http transactions. Then use any browser to login in the usual way as you do it online and then in WireShark you will see all the requests and responses between your browser and server. Find the one you are interested in by the known IP address or the host address and then copy the readable bytes which you find if you click the right button on any of the transactions. So when you do that, you will find how your request to the server must look like and which values are needed.
Back to coding(java):

public HttpResponse httpPost1(String viewstateValue, String url, String username, String password)
            throws ConnectTimeoutException {
            try {
                // --------post
                HttpPost httppost = new HttpPost(url);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE",
                        viewstateValue));
                nameValuePairs.add(new BasicNameValuePair("__EVENTTARGET", ""));
                nameValuePairs
                        .add(new BasicNameValuePair("__EVENTARGUMENT", ""));

                nameValuePairs.add(new BasicNameValuePair("ctl00$tbUsername",
                        username));
                nameValuePairs.add(new BasicNameValuePair("ctl00$tbPwd", password));
                nameValuePairs.add(new BasicNameValuePair(
                        "ctl00$chkRememberLogin", "0"));
                nameValuePairs.add(new BasicNameValuePair("ctl00$cmdLogin",
                        "Login"));
                // nameValuePairs.add(new
                // BasicNameValuePair("ctl00$cmdForgetMe",
                // "Forget Me"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = client.execute(httppost);

                String responseHtml = EntityUtils.toString(response
                        .getEntity());
                // System.out.println(responseHtml);
                // System.out.println(response1.getStatusLine());

            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }

        return response;
    }

Here i post the values to the URL which i know in advance.

You might add headers using

httppost.addHeader("Referer",
                    "http://website/login.asp");

or time-out value to the request

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            int timeoutConnection = 5000;
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);

In this case it is better to catch the exceptions if the timeout occured and make the request again, as it is advised to do in the HttpClient documentation.

HttpClient follows redirects by default, but it is possible to catch everytime the redirect occurs by using:

private RedirectHandler customRedirectHandler;

........
(maybe constructor..)
    client.setRedirectHandler(customRedirectHandler);
........
class CustomRedirectHandler extends DefaultRedirectHandler {
        @Override
        public boolean isRedirectRequested(HttpResponse response,
                HttpContext context) {
            // System.out.println("isRedirectRequested");

            return true;
        }

        @Override
        public URI getLocationURI(HttpResponse response, HttpContext context)
                throws ProtocolException {

            String location = response.getLastHeader("Location").getValue();

            if (location.contains("Login")) {
                // System.out.println("Login needed");
            } else {
                // System.out.println("No login required");
            }

            URI redirectURI = null;
            try {
                redirectURI = new URI(location);
            } catch (URISyntaxException e) {
            }

            return redirectURI;
        }
    }
}

and then after doing everything that you need with the client you might release your clients connection:

public void shutDownClient() {
    client.getConnectionManager().shutdown();
}

This is an example of the HttpClient, do not forget that you might also use the UrlConnection, here are differences shown:

http://www.innovation.ch/java/HTTPClient/urlcon_vs_httpclient.html

So it depends what you prefer to use and what is more appropriate for your project.

P.S. This POST solution is applied to my project, but it may not work for yours.. Use Wireshark first and then see what kind of requests must be sent to the server. In my case it was like viewstate=sdsgdgdfd323&username=dsfngkjfdg&password=dsfsdfsfs….. but i know that there might be others.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:46:11+00:00Added an answer on May 23, 2026 at 8:46 pm

    Whether or not the URL accepts a PUT in place of a POST depends entirely on the server.

    Most servers that expect a POST will accept only a POST. A PUT is used much more rarely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if an applet made in java, can send data to
I am writing a web server in Java and I want it to support
I wonder why would a C++, C#, Java developer want to learn a dynamic
In a Java application I want to be able to take a timestamp at
From within my Java program I want to determine which .NET Framework is installed
I have a little Java problem I want to translate to Python. Therefor I
I am making a game in JAVA where I want to come up with
I want to develop Java apps, real quick, what IDE should I choose?
I want to use java.util.ConcurrentLinkedQueue as a non-durable queue for a Servlet. Here's the
I want to create a Java application bundle for Mac without using Mac. According

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.