• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need some clarity between httpUrlconnection and sockets?

akits

Lurker
I am confused between sockets and HttpUrlconnection classes in java.

What i know that sockets works on TCP and handles connection between two computers while the HttpUrlconnection handles Http Requests.

My questions :
1. connection using sockets involves Client socket object and a serverSocket intialized at server. What i am not understanding that in Urlconnection on connecting using this Is it possible to connect using a HttpUrlconnection request from Client to a server which is using serverSocket ?
Here is what i am doing:

public class ClientUsingHttp {
public static void main(String args[]){
try{
DataInputStream inputStream = null;
HttpURLConnection urlConnection = null;
URL url=new URL("http://localhost:8080/");
//to localhost at port 8080 which is opened by myServer socket

urlConnection=(HttpURLConnection)url.openConnection();
inputStream=(DataInputStream)urlConnection.getInputStream();
String str=inputStream.readUTF();
System.out.print(str);
}catch(Exception e){
System.out.println("error"+e);
}
}
}

This is my server class:
public class MyServer {
public static void main(String args[]) throws IOException{
try{
ServerSocket ss=new ServerSocket(8080);
System.out.println("server ready");
Socket s=ss.accept();

DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello httpURlConnection Client");
dout.flush();
ss.close();
}catch(Exception e){System.out.println("throws Exception "+e);
}
}
}

this is my Client through Urlconnection:

After running the `myServer` first it works fine and showing the expected message "server ready" and then running `ClientUsingHttp` gives error as
`errorjava.io.IOException Invalid Http Response`

I studied and found some examples where we are sending HttpUrlconnection request and sending Json to server but since json is accepted by any language so they are accepting that json object using PHP script. but i want to know how to handle a simple text data in java at server or Website.

2: Can you tell how a HttpUrlConnection request is handled at the server or Website(here is another question: Is website acts as a server if we are connecting to a url) and how to handle HttpUrlConnection request from client to server in java?

3: Is `HttpUrlConnection` is only a client side class.

4: can you tell can we use where we place the java server we will create to handle HttpUrlConnection ?

5: can multiple clients connects to a same server in case of sockets or Java server for handling HttpUrlconnection requests?

I think i am confused between TCP and Http (even though i read about them a lot on the net), and UrlConnection and sockets;

Please can you help me understanding this concept, it will be great. Or can you give me links to good resources?

i also like you to tell you what can be use for following steps below:

for example: we take browser as Client
for 2: we create a sockets to connect to port 80 and to its url as socket = new Socket("www.google.com",80);
foe 3: how the server sends Http requests to server if using sockets for connection;
A basic Web browsing session works as follows:

1: the user specifies a URL in their browser (either from a bookmark or by typing)
2: the browser initiates a TCP connection to the Web server (or server pool) via its IP address as published in DNS. (Web servers by default use TCP port 80 to service incoming requests.). As part of this process, the browser also makes DNS lookup requests to convert the URL to an IP address
3: after the server completes acknowledgment of its side of the TCP connection, the browser sends HTTP requests to the server to retrieve content for the URL.
4: after the server replies with content for the Web page, the browser retrieves the content from the HTTP packets and display it accordingly.

Content can include embedded URLs for advertising banners or other third-party content, that in turn triggers the browser to issue new TCP connection requests to those locations. The browser may also save temporary information about its connections to local files on the client computer called cookies.
 
Last edited by a moderator:
Well to address the specific problem of why you are getting "Invalid HTTP response", it's because what you're writing to the output stream isn't valid. A minimal HTTP response is this

Code:
String response = "HTTP/1.1 404 Not Found\r\n" +
   "Content-Length: 22\r\n" +
   "Content-Type: text/html\r\n\r\n" +
   "<h1>404 Not Found</h1>";
dout.write(response.getBytes());
 
Back
Top Bottom