0

update : this is a duplicate,

i'm building a Proxy-custom-tag with grails taglib, per default it makes a get-request, now i'm facing the problem, that it should be able to handle Post-requests too,# and i'm able to check the request-method and conditionally set the openConnection method to post if necessary, but i dont know how to append the post-params to the request. here 's my code so far

def wordpressContent = { attrs, body ->
    def url
    def requestMethod = request.getMethod()
    def queryString = request.getQueryString()?'&'+request.getQueryString():''  
    def content
    println "method :"+requestMethod
    println "params == "+params   // <- inside here are the post-parameters
    url = grailsApplication.config.wordpress.server.url+attrs.pageName+'?include=true'+queryString  
    try {
        content = url.toURL().openConnection().with { conn ->
            if(requestMethod == 'POST'){
                println "Its a POST"
                conn.setRequestMethod("POST")
                conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    // HOW to append the params here ? 


            }
            readTimeout = 6000
            if( responseCode != 200 ) {
                throw new Exception( 'Not Ok' )
            }
            conn.content.withReader { r ->
                r.text
            }
        }
    }
    catch( e ) {
        println "exception : "+e
        content="<div class='float' style='margin-top:10px;width:850px;background-color:white;border-radius:5px;padding:50px;'>Hier wird gerade gebaut</div>"
    }
    out << content
}

im very stuck here right now, i found answers saying to use this syntax

Writer wr = new OutputStreamWriter(conn.outputStream) 
wr.write(postParams) 
wr.flush() 
wr.close() 

but i dont know how to include that to my existing code, for any hints thanks in advance

update: my solution was to build up the post-parameter-querystring by iterating over the params object in this pattern "xyz=zyx&abc=cba" and write it to the outputStream like above

john Smith
  • 17,409
  • 11
  • 76
  • 117
  • possible duplicate of [Java - sending HTTP parameters via POST method easily](http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) – tim_yates Dec 03 '13 at 15:08
  • yes it is, i updated the question to point that out, my special solution was just to build up the post-param string and write it to the outputStream – john Smith Dec 04 '13 at 20:06

1 Answers1

2
 // HTTP POST request
private void sendPost() throws Exception {

    String url = "https://selfsolve.apple.com/wcResults.do";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add request header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());

}

you are using grails so you can also use groovy HTTPBuilder like below

http://groovy.codehaus.org/modules/http-builder/doc/

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Deepak
  • 2,287
  • 1
  • 23
  • 30