🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #14421

Re: How do I send parameters via doPost from java?

#14421
From: Thomas Muller
Date: Tue, 07 Sep 1999 00:00
40 lines
2254 bytes
Jason Dore wrote in message
<936710431.2510.0.nnrp-03.9e984195@news.demon.co.uk>...
>Hi
>
>I cant find any reference in the Java documentation to the mechanism I need
>to use to open a 'POST' style URLConnection to a web server and pass
>parameters as if they were coming from a web form.
>
>

Assume data comes in a java.util.Map as key/value pairs.

Map data = getParametersFromSomewhere();

StringBuffer buffer = new StringBuffer(100);
for (Iterator i = data.keySet().iterator(); i.hasNext();) {
    Object key = i.next();
    buffer.append(key.toString() + "=" +
URLEncoder.encode((data.get(key)).toString()) + "&");
}
String postString = buffer.toString();
URL url = new URL( "..." );
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
// A content-length header must be contained in a POST request
connection.setRequestProperty("Content-length", postString .length());
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
out.write(postString.getBytes());
out.close();
InputStream in = new BufferedInputStream(connection.getInputStream());
int statusCode = connection.getResponseCode();
processInput(in);
in.close();

--

Thomas
Technical Details
Message-ID: <WsbB3.377$6O2.500@news1.online.no>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <936710431.2510.0.nnrp-03.9e984195@news.demon.co.uk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,59447e8c14221473
X-Google-Attributes: gid10a146,public
X-Google-Thread: f74b3,59447e8c14221473
X-Google-Attributes: gidf74b3,public
From: "Thomas Muller" <ttm@online.no>
Subject: Re: How do I send parameters via doPost from java?
Date: 1999/09/07
Message-ID: <WsbB3.377$6O2.500@news1.online.no>
X-Deja-AN: 522170517
Content-Transfer-Encoding: 7bit
References: <936710431.2510.0.nnrp-03.9e984195@news.demon.co.uk>
Content-Type: text/plain; charset="iso-8859-1"
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
X-Complaints-To: news-abuse@online.no
X-Trace: news1.online.no 936723382 148.122.202.102 (Tue, 07 Sep 1999 18:56:22 MET DST)
Organization: Telenor Online Public Access
MIME-Version: 1.0
NNTP-Posting-Date: Tue, 07 Sep 1999 18:56:22 MET DST
Newsgroups: borland.public.jbuilder.java.language,comp.lang.java.programmer