Luis M
Luis M

Reputation: 51

convert java example from GET to POST

im not experienced in java and i got a working example code for some XML interaction, my only problem is that i need to POST data (username/password)

the relevant code is:

static String myUrlString = "http://www.grupoandroid.com/interface/mobile/index.php";
protected TheParser(){
    try {
        this.myUrl = new URL(myUrlString);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}



protected InputStream getInputStream() {
    try {
        return myUrl.openConnection().getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 0

Views: 1051

Answers (2)

Mighty
Mighty

Reputation: 11

I think you're better off using an http-client library, rather than parsing the full http stack yourself.

apache-http-client
hotpotato

You can find enough examples and tutorials in the web for these.

Upvotes: 0

ratchet freak
ratchet freak

Reputation: 48176

you can use HttpURLConnection

protected InputStream getInputStream() {
    try {
        HttpURLConnection con = new HttpURLConnection(myUrl);
        con.setRequestMethod("POST");
        //writeout data to the output stream
        return con.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 1

Related Questions