4 Replies - 257 Views - Last Post: 31 January 2013 - 04:07 PM Rate Topic: -----

#1 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

HTTP as string parser[problem]

Posted 30 January 2013 - 04:18 PM

Hey there, I'm new to java and I wanted to try a simple socket program, but I'm having difficulties with a certain part of it.

I have a string like this:
GET http://www.google.com/ HTTP/1.1



Which I got by sending the browser request to my program(a proxy basically). What I want to do now is connect through a socket to the host, in this case google.com, and send back the response I get to the browser so the html would show up.

My problem is in the parsing of the "HTTP request", does anyone knows about a class that could parse that string? So I wouldn't need to spend time making a parsing class, as it's not my focus in that program.

If anyone could help I'd be glad, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: HTTP as string parser[problem]

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,392
  • Joined: 27-December 08

Re: HTTP as string parser[problem]

Posted 31 January 2013 - 08:07 AM

Why do you need to write a proxy in Java? Google really doesn't like third parties directly querying and scraping their site, by the way.
Was This Post Helpful? 0
  • +
  • -

#3 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: HTTP as string parser[problem]

Posted 31 January 2013 - 09:12 AM

I'm just trying things out, the host does not need to be google exactly, it can be any other site.

I just want some class that could parse the string:
GET http://www.google.com/ HTTP/1.1


Into:
host: http://www.google.com/



So that than I could do something like this:
//java socket client example
import java.io.*;
import java.net.*;

public class socket_client
{
    public static void main(String[] args) throws IOException 
    {
        Socket s = new Socket();
	String host = "www.google.com";
	PrintWriter s_out = null;
	BufferedReader s_in = null;
		
        try 
        {
		s.connect(new InetSocketAddress(host , 80));
		System.out.println("Connected");
			
		//writer for socket
            s_out = new PrintWriter( s.getOutputStream(), true);
            //reader for socket
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        }
        
        //Host not found
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        }
        
        //Send message to server
	String message = "GET / HTTP/1.1\r\n\r\n";
	s_out.println( message );
			
	System.out.println("Message send");
		
	//Get response from server
	String response;
	while ((response = s_in.readLine()) != null) 
	{
		System.out.println( response );
	}
    }
}



I just didn't want to go through the trouble of writting the parser myself, but if no one knows about such a functionality already I could just do it.

This post has been edited by shurd: 31 January 2013 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,392
  • Joined: 27-December 08

Re: HTTP as string parser[problem]

Posted 31 January 2013 - 09:18 AM

See the String split() method to break apart the String into tokens.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,167
  • Joined: 20-September 08

Re: HTTP as string parser[problem]

Posted 31 January 2013 - 04:07 PM

There's no need to 'roll your own' http using a raw Java Socket. See HttpURLConnection
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1