Wednesday, October 14, 2009

Post method in java

public String postObject(String url,String object)
       {

          String resultsPage="";
         try{
              String urlStr=url;
              String val=object;
              URL postURL = new URL( urlStr );
              HttpURLConnection con = (HttpURLConnection) postURL.openConnection();
              con.setUseCaches(false);      
              con.setDoOutput(true);        
              con.setDoInput(true);         
              con.setRequestMethod("POST");  
              PrintWriter out = new PrintWriter(con.getOutputStream());
              String postStr = "link="+URLEncoder.encode(val);
              out.println(postStr); 
              out.close();
              String inputLine="";
        BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream()));
              while ((inputLine = in.readLine()) != null)
                   resultsPage+=inputLine;
              in.close();
              System.out.println(resultsPage); //Final output
          }
         catch(Exception ex)
          {
             System.out.println(ex);
          }

      return resultsPage;
       }

No comments:

Post a Comment