File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Sockets vs URL Based classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sockets vs URL Based classes" Watch "Sockets vs URL Based classes" New topic
Author

Sockets vs URL Based classes

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
when to use each? any suggestion, rules...
for. ex. I cannot connect to www.yahoo.com via Socket("http://www.yahoo.com",80); way but w the URLConnection.
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
HTTP is used when you need to connect to the internet through corporate firewalls.
Chris De Vries
Ranch Hand

Joined: Dec 05, 2002
Posts: 65
[list]Socket takes an internet address... http://www.yahoo.com is a URL.So the code: wont work anyway. You can connect to port 80 of www.yahoo.com using .[/list]
  • HTTP is Hypertext Transfer Protocol. It is a protocol developed to transfer web pages from a http server such as apache or IIS to a browser. HTTP is also now used to transfer other things as well, such as images, xml files, etc... Another example of a protocol is FTP or File Transfer Protocol. Neither was developed specifically to bypass corporate firewalls.


  • As a rule of thumb I would say use URLs when you can, and Sockets when you can't use URLs.
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    Here a HTTP connection as well as the Direct Socket connection are using the Socket class.
    I'm trying to interpret this code and coming into wrong conclusions. Any light thrown would be appreciated.
    Chris De Vries
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 65
    It looks like this code either connects to the remote host or to the remote proxy, though it never sends anything to the proxy to indicate that it should forward the connection to the remote host. Also, I don't know why the program insists on specifying the local address and port.
    The comments indicate that this is an HTTP proxy, so to use it (without specifically connecting to the proxy and using it's protocol directly) you may want to set the following properties:
    http.proxySet=true
    http.proxyHost=proxyAddress
    http.proxyPort=proxyPort
    And then use a URLConnection with the remote host address. It will automatically use the HTTP proxy.
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    Thanks for your response.
    I guess I have a problem with the authentication though.

    gives me the error
    java.io.IOException: Server returned HTTP response code: 407 for URL: http://www.marchal.com/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:709)
    at net1.url2.URLTrial.main(URLTrial.java:43)
    Server returned HTTP response code: 407 for URL: http://www.marchal.com/

    I think this has to do with authorizing the server. Can you please include the statement to authorize in my code,
    Thanks,
    Divya
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    I mean authorizing the proxy...
    Chris De Vries
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 65
    You might want to try the Jakarta Commons HttpClient API to connect through your authenticating http proxy server.
    The SSL guide has a snippet of code relating to authenticating proxies.
    Chris
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    Could you tell me how to download the the Jakarta packages and make those Jbuilder configured?
    Do I have to download the jars from the link HttpClient on ibiblio from the page
    http://jakarta.apache.org/commons/httpclient/downloads.html
    Thanks for your help,
    Divya
    Chris De Vries
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 65
    At this point I would encourage you to read the documentation for both HttpClient and JBuilder. I have never used JBuilder, and I do not recall if you only need to download HttpClient jars or if other jakarta jars are also required, but I do know that it's all covered in the scant documentation.
    Chris
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    Hi Chris,
    I could configure my Jbuilder to use the Jakarta HttpClient. I'm able to connect to the web site and the proxy authenticated with no problems for
    the Web page that doesn't send a cookie.
    But for the page that sends a cookie,
    it gives me the following error.
    Oct 8, 2003 1:12:14 PM org.apache.commons.httpclient.HttpMethodBase processAuthenticationResponse
    INFO: Already tried to authenticate to "courtgate.coca.co.clark.nv.us#" but still receiving 407.
    <HTML>
    <HEAD><TITLE>407 Proxy Authentication Required</TITLE></HEAD>
    <BODY>
    <H1>Proxy Authentication Required</H1>
    <H4>
    Unable to complete request:<P>
    Access denied due to authentication failure.

    </H4>
    <HR>
    </BODY>
    </HTML>
    Any ideas?
    Thanks for helping me so far,
    Divya
    Chris De Vries
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 65
    Hmm... Well at least it's partly solved. I'm not sure why cookies would be a problem though. I must admit I have never tried using an authenticating proxy server with a java client, so I have not run into this problem.
    You may want to ask this question on the Jakarta Commons user email list or developer email list. The mailing list archives can also be searched, though a quick search I did, didn't reveal exactly this problem. I'm sure they would be happy to receive your input and fix it if it turns out to be a bug.
    Chris
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    I've just subscribed to the mailing list of JakartaCommonsHTTPClient. thnaks for the suggestion. I wonder why didn't I do this before!!!
    well, looks like it's gong to take some time before they answer me though.
    In the mean time can you throw some light on how can I modify my code to recieve and send a cookie through HTTPClient. I mean I went through the documentation but was of no help.
    Thanks,
    Divya
    Idly Vada
    Ranch Hand

    Joined: Sep 02, 2003
    Posts: 135
    I don't think a seperate package is necessary to provide proxy authentication support to your app.
    You can use any base64 encoder to encode proxy username and password.And put this value for Proxy-authorization request property.There are lot of base64 encoders available. sun VM has one in sun.misc package.
    Chris De Vries
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 65
    Hmm... It may be that you don't need HttpClient after all. As I was looking for Base64 encoding, I came across java.net.Authenticator.
    It looks like you may be able to create a proxy password authenticator and use it as the default authenticator for the proxy. This code snippet might work:

    I have not tried that code, but something like it might work.
    Chris
    divya madala
    Ranch Hand

    Joined: Jan 10, 2001
    Posts: 61
    Chris,
    I suceeded with the proxy with the simple HttpConnection.
    I did exactly what you guys have suggested.
    Authenticator.setDefault(new SimpleAuthenticator(username, password));
    Properties systemProperties = System.getProperties();
    systemProperties.setProperty("http.proxyHost", proxy);
    systemProperties.setProperty("http.proxyPort", port);
    But my problem is to capture the cookie and send it to the website.
    Like say I capture the cookies in a vector and send the last recent cookie when I access the website again to keep track of my sessions.
    Thanks,
    Divya
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Sockets vs URL Based classes
     
    Similar Threads
    Applications of Portal/portlet in the industry
    jstl c + import
    Problem in reading URL
    Is there a way to read HTML into a string from a URL
    Astrology