POST from HttpsURLConnection

Posted on 2006-04-06
Last Modified: 2012-05-05
I am trying to create web client for  form-based logging  .
here is the piece of code I am using.
All values I am getting by parsing start up form.
When looking at Post request in IEWatch one line is different:
(Method-Line)      POST /c/a/WirelessLogIn HTTP/1.1
I am using just       connection.setRequestMethod("POST");   without any parameters
I am not sure  if this makes any difference.

Here is what  I am posting:
      String body    ="USERID="+v_account+"&PASSWORD="+v_pin
      url = new URL(t_url+body);
      connection = (HttpsURLConnection)url.openConnection();
      //connection.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/, application/msword, */*");
      connection.setRequestProperty("Accept", "*/*");
      connection.setRequestProperty("Referer", "");
      connection.setRequestProperty("Accept-Language" , "en-us");
      connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
      connection.setRequestProperty("Content-Type" , "application/x-www-form-urlencoded") ;
      connection.setRequestProperty("User-Agent",  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)");
      connection.setRequestProperty("Host", "");
     // connection.setRequestProperty("Content-Length","120");    
      connection.setRequestProperty("Connection", "Keep-Alive");
      connection.setRequestProperty("Cache-Control", "no-cache");
      connection.setRequestProperty("Cookie", cookie);

 The response is :

<TITLE>Page Unavailable</TITLE><BODY>
/html/errors/404.html is unavailable, please contact system administractor.

As you can see I've commented out one line       // connection.setRequestProperty("Content-Length","120");  
because with this line the sessin hangs and I am getting no response at all .
How to solve this issue ???

Question by:lez60
    LVL 92

    Accepted Solution

    >     String body    ="USERID="+v_account+"&PASSWORD="+v_pin
    >                     +"&login=Login&pagehandler=PHWirelessLogIn&DV_DATA="+v_data+"&USERGROUP=ACCT&COMPANY=ABCD";

    your passing the parameters as a GET.
    For a post you need to instead write them to the connections output sytream

    Author Comment

    Yes !!!  It works , Thanks !!. I've seen this example before but it looks like not quite got it before. POST and GET is a little bit confusing,
    somtimes I was using GET method to login, and it worked, but in this situation it had to bePOST .

    Great, I am really approciated

    LVL 92

    Expert Comment

    no worries :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now