We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › HTTP Client - End of content
Page Index Toggle Pages: 1
HTTP Client - End of content (Read 1054 times)
HTTP Client - End of content
Dec 15th, 2006, 3:26am
 
Anyone got an easy way to definitively identify the end of the content in an HTTP request if the server doesn't include 'content-length' in the header?  Is there a close socket message I can detect in the INet library?
Re: HTTP Client - End of content
Reply #1 - Dec 15th, 2006, 10:38pm
 
May be this can inspire you.
From Java P2P Unleashed. Download the code at the bottom.

Code:


import java.io.*;
import java.net.*;

public class HttpConnection {

private String file_name,host_name;
private Socket httpSocket=null;
private StringBuffer HTTPRequest;
private StringBuffer requestProperties = new StringBuffer();
private InputStream in = null;
private OutputStream out = null;
String contentType;
String requestMethod;
String HTTPRequestPayload;


//constructor
public HttpConnection ( String url,
int port,
String contentType,
String requestMethod,
String HTTPRequestPayload
) throws UnknownHostException, IOException {

HTTPRequest = new StringBuffer();

//Resolve the user passed URL
//into host and file names.
String subStringWithOutHTTP = url.substring (7);
int slashPosition = subStringWithOutHTTP.indexOf ("/");
file_name = subStringWithOutHTTP.substring (slashPosition);
host_name = subStringWithOutHTTP.substring (0,slashPosition);

httpSocket = new Socket(host_name,port);

this.contentType= contentType;
this.requestMethod = requestMethod;
this.HTTPRequestPayload = HTTPRequestPayload;

}//HttpConnection()


//Sets a request property.
public void setRequestProperty (String name, String value){
if (requestProperties.capacity()==0) {
requestProperties = new StringBuffer();
}
requestProperties.append(name+" "+value+"\r\n");
}// end setRequestProperty()

//Send the HTTP request synchronously.
public String call(){

HTTPRequest.append(requestMethod + " "+file_name+" HTTP/1.1\r\n");
HTTPRequest.append("Host: "+host_name+"\r\n");
HTTPRequest.append("Content-type: "+ contentType +
"; charset=utf-8\r\n");
HTTPRequest.append("Content-length:"
+ String.valueOf(HTTPRequestPayload.length())
+ "\r\n");
String HTTPRequestString = HTTPRequest.toString();

requestProperties.append("\r\n");
HTTPRequestString +=requestProperties.toString();
HTTPRequestString +=HTTPRequestPayload;

try{
PrintWriter out =
new PrintWriter(httpSocket.getOutputStream());
BufferedReader in =
new BufferedReader(
new InputStreamReader(httpSocket.getInputStream()));

// sending request to output stream
out.println(HTTPRequestString);
out.flush();

StringBuffer sc = new StringBuffer();
String line = null;

while((line=in.readLine())!=null){
sc.append(line);
sc.append("\r\n");
}

// closing input and output streams
String response = sc.toString();
in.close();
out.close();

return response;

}catch (Exception e){
return null;
}//catch
}// end getHeaders()

}// end class


Re: HTTP Client - End of content
Reply #2 - Dec 16th, 2006, 9:19pm
 
Excellent thanks.  I'm coming over to Java / Processing from C++ and am used to using the WinINet API.  This is perfect.
Page Index Toggle Pages: 1