public class HttpServiceClient
{
private static final String HTTP_VERSION = "1.0";
private static final String HTTP_POST_REQUEST =
"POST";
private static final String HEADER_HOST = "Host";
private static final String HEADER_CONTENT_TYPE =
"Content-Type";
private static final String XML_MIME_TYPE =
"text/xml";
private static final String
HEADER_CONTENT_LENGTH = "Content-Length";
private static final int DEFAULT_PORT = 80;
private String serviceUrl;
private int returnCode;
private String returnMessage;
private Reader responsePayload;
public HttpServiceClient( String serviceUrl)
{
this.serviceUrl = serviceUrl;
}
public ServiceResponse executeRequest(
ServiceRequest request)
throws HttpServiceException
{
try
{
String data = request.serializeRequestToString(
"utf-8");
postRequest(data);
//check for failures
if(returnCode != 200)
throw new HttpServiceException(
returnMessage);
InputSource source =
new InputSource(responsePayload);
DOMParser parser = new DOMParser();
parser.parse(source);
ServiceResponse serviceResponse =
new ServiceResponse(parser.getDocument());
String theResponse =
serviceResponse.serializeResponseToString(
"utf-8");
System.err.println(theResponse);
return serviceResponse;
}
catch(Exception ex)
{
ex.printStackTrace(System.err);
throw new HttpServiceException(
ex.getMessage());
}
}
private void postRequest(String payload)
throws HttpServiceException
{
PrintWriter out = null;
BufferedReader in = null;
URL url = null;
try
{
url = new URL(serviceUrl);
// No port? use default port 80
int port = url.getPort () < 0 ? DEFAULT_PORT :
url.getPort();
Socket soket = new Socket (
url.getHost (), port);
out = new PrintWriter (
soket.getOutputStream ());
in = new BufferedReader (
new InputStreamReader (
soket.getInputStream ()));
}
catch (Exception ex)
{
throw new HttpServiceException (
"error opening socket: " + ex.getMessage ());
}
out.print (HTTP_POST_REQUEST + " " +
url.getFile() +
"HTTP/" + HTTP_VERSION + "\r\n");
out.print (HEADER_HOST + ": " + url.getHost () +
':' + url.getPort () + "\r\n");
out.print (HEADER_CONTENT_TYPE + ": " +
XML_MIME_TYPE + "\r\n");
out.print (HEADER_CONTENT_LENGTH + ": " +
payload.length () + "\r\n");
out.print ("\r\n");
out.print (payload);
out.print ("\r\n\r\n");
out.flush ();
try
{
String statusLine = in.readLine();
System.err.println(statusLine);
parseStatusLine(statusLine);
}
catch (Exception ex)
{
throw new HttpServiceException ("error parsing
HTTP status line: " + ex.getMessage ());
}
// I will ignore all the headers and keep reading
// until I get an empty line
try
{
String headerLine = null;
while ((headerLine = in.readLine ()) != null)
{
if (headerLine.length () == 0)
break;
}
}
catch (Exception ex)
{
throw new HttpServiceException ("error reading
HTTP headers: " + ex.getMessage ());
}
//what remains of the input Stream is my payload
responsePayload = in;
}
private void parseStatusLine(String statusLine)
throws Exception
{
StringTokenizer st =
new StringTokenizer (statusLine);
// this is the HTTP Version
st.nextToken ();
returnCode = Integer.parseInt (st.nextToken ());
StringBuffer retMessage = new StringBuffer ();
while (st.hasMoreTokens ())
{
retMessage.append (st.nextToken ());
if (st.hasMoreTokens ())
{
retMessage.append (" ");
}
}
returnMessage = retMessage.toString ();
}
}
Listing 2 The HttpServiceClient class handles all the details of the HTTP
request. You do, however, need to know the exact format of the request.
For more details look at
http://www.xmlmag.com/upload/free/features/xml/2001/05may01/dn0102/dn0102.as
p
Good luck,
Bala
-----Original Message-----
From: Stuart Subscription [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 30, 2001 12:35 PM
To: [EMAIL PROTECTED]
Subject: Send Post using Socket
Servlet List,
Can anyone demonstrate with a code snippet how to send a http POST using a
new socket?
I already have the following code example that sends a http GET using
sockets.
java.net.Socket socket = new
java.net.Socket(request.getServerName(),
request.getServerPort() );
socket.setSoTimeout(timeout);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("GET /servlet/" +servletName+ " HTTP/1.0);
out.println("");
Regards
... Stuart
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html