KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > PostMethod


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URLEncoder JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  *
29  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  */

31 public class PostMethod extends HttpMethod {
32
33     public PostMethod(String JavaDoc uri) {
34         super("POST", uri); //$NON-NLS-1$
35
}
36
37     public HttpResponse execute(HttpRequest request, HttpConnection con) throws IOException JavaDoc {
38
39         request.setHeaderField("Host", con.getHostHeaderValue()); //$NON-NLS-1$
40
request.setHeaderField("User-Agent", HttpClient.USER_AGENT); //$NON-NLS-1$
41

42         // Prepare request body
43
String JavaDoc encodedParams = ""; //$NON-NLS-1$
44
for (Enumeration JavaDoc e = getParameterNames(); e.hasMoreElements();) {
45             String JavaDoc name = (String JavaDoc) e.nextElement();
46             Vector JavaDoc values = getParameterValueList(name);
47             for (Enumeration JavaDoc e2 = values.elements(); e2.hasMoreElements();) {
48                 String JavaDoc value = (String JavaDoc) e2.nextElement();
49                 encodedParams += (encodedParams.length() > 0 ? "&" : "") + URLEncoder.encode(name) + "=" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
50
URLEncoder.encode(value);
51             }
52         }
53
54         byte[] body = encodedParams.getBytes();
55         request.setHeaderField("Content-Type", "application/x-www-form-urlencoded"); //$NON-NLS-1$ //$NON-NLS-2$
56
request.setHeaderField("Content-Length", String.valueOf(body.length)); //$NON-NLS-1$
57

58         request.performRequest(this, con);
59
60         con.getOutputStream().write(body);
61
62         return new HttpResponse(con);
63     }
64 }
65
Popular Tags