HTTP request(LoadURL) program problem(file connectable but not readable)

I have the following code:
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class LoadURL {
     public LoadURL(String urlRequest) {
          SocketChannel sock=null;
          try {
               URL url=new URL(urlRequest);
               String host=url.getHost();
               String page=url.getPath();
               InetSocketAddress address=new InetSocketAddress(host,80);
               Charset iso=Charset.forName("UTF-8");
               CharsetDecoder decoder=iso.newDecoder();
               CharsetEncoder encoder=iso.newEncoder();
               ByteBuffer byteData=ByteBuffer.allocate(16384);
               CharBuffer charData=CharBuffer.allocate(16384);
               sock=SocketChannel.open();
               sock.configureBlocking(false);
               sock.connect(address);
               Selector listen=Selector.open();
               sock.register(listen, SelectionKey.OP_CONNECT + SelectionKey.OP_READ );
               while (listen.select()>0){
                    Set keys=listen.selectedKeys();
                    Iterator i=keys.iterator();
                    while (i.hasNext()){
                         SelectionKey key=(SelectionKey) i.next();
                         i.remove();
                         SocketChannel keySock=(SocketChannel)key.channel();
                         if (key.isConnectable()){
                              System.out.println("Connectable");
                              if (keySock.isConnectionPending()){
                                   keySock.finishConnect();
                              CharBuffer httpReq=CharBuffer.wrap("GET" + page + "\n\r\n\r");
                              ByteBuffer request=encoder.encode(httpReq);
                              keySock.write(request);
                         else if (key.isReadable()){
                              System.out.println("Readable");
                              keySock.read(byteData);
                              byteData.flip();
                              charData=decoder.decode(byteData);
                              charData.position(0);
                              System.out.print(charData);
                              byteData.clear();
                              charData.clear();
          sock.close();
          } catch (MalformedURLException mue){
               System.out.println(mue.getMessage());
          }catch (UnknownHostException uhe) {
               System.out.println(uhe.getMessage());
          }catch (IOException ioe){
               System.out.println(ioe.getMessage());
     public static void main(String arguments[]) {
          LoadURL app=new LoadURL(arguments[0]);
It compiles without any problems but when I run it (example: > java LoadURL http://www.java.sun.com) it does nothing. I think the problem is that the key.isReadable() (line 51 of the code) ,at the else if check, returns false when it should return true and so it doesn't enters the loop. I'd appreciate any help here.Thank you.

Why are you reinventing the wheel? Use the URLConnection class.

Similar Messages

  • Problem: Socket connection is not creating in machine, through utility program (MFC Dll), on ListDisplay service port - 3334 (on separate machine), while we are able to telnet on same ListDisplay service port - 3334 from same issue machine on same time

    Problem: Socket
    connection is not creating in machine, through utility program (MFC Dll), on ListDisplay service port - 3334 (on separate machine), while we are able to telnet on same ListDisplay service port - 3334 from same issue machine on same time
    Environment: -
    OS:
    Windows XP SP2/7
    Code:
    VC 6.0
    Dll: MFC
    Problem Description: -
    We have written a utility program which create socket (Using windows standard method [MFC]), and then make connection with another service (List Display) running
    on port 3334 in different machine and retrieve the required list data. This program was working fine in almost all the machines.
    But, we have received a severe intermittent issue on two machines. Client is facing issue in displaying the list data from port 3334.
    Attempt: -
    First we tried to debug code, and we come to know that socket is not creating in utility program. So we tried to telnet on ListDisplay service port 3334 and we were surprised that we were able to telnet, then we opened some more
    telnet window on same port 3334 around (6 to 8) window, and each cmd connected properly. But we were not able to create socket from utility program.
    Problem is severe because issue is intermittent.
    We have tried all the way, but we are not able to figure it out, that what can be the exact problem and what are the conditions, when utility program will not
    connect with ListDisplay service on port 3334.
    Kindly assist to resolve this issue. For any help, we would be really thankful.

    Hi,
    According to your description, it seems that you have created an utility program which is making connection with another service port 3334, however, two clients are facing issue in display the data list from port 3334.
    Port: 3334/TCP
    3334/TCP - Known port assignments (1 record found)
    Service
    Details
    Source
    directv-web
    Direct TV Webcasting
    IANA
    Since the port 3334 is used by directv-web service, I'd like to suggest check this service it is working well on the problematic clients.
    1. The client can be resolved in DNS well? Please run "nslookup" in the prompt command.
    2. Is there any 3rd party application interrupting? Do test in clean boot.
    2. Strongly suggest you run process monitor tool to analysis it.
    I am looking forward to your reply if you have any updated on your side.
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

  • TS1389 Trying to play purchased TV series to my Apple TV, I am requested to authorise the computer.  This has already been done and I have tried the suggestions regarding authorisation.  I can play the programs on my computer, but not the Apple TV.

    Trying to play purchased TV series to my Apple TV, I am requested to authorise the computer.  This has already been done and I have tried the suggestions regarding authorisation.  I can play the programs on my computer, but not the Apple TV.
    Any ideas?

    in remote app click on the
    lower left corner on the text
    top of the context menu click devices
    choose the appletv rather then your shared lib in the list
    if you are trying to view a movie that is located on your ipad on your tv using the appletv then
    you should use airplay mirror not the remote app

  • ITunes crashes on exit when iPod is connected, but not otherwise?

    I had this problem ever since I installed iTunes 7...
    It occoured as soon as I installed the new verison while still using my old 4gb first gen. iPod nano.
    Now I just recently bought a new second generation nano, and still have the same problem.
    And the issue is, Whenever I close iTunes while my iPod is connected (but not being updated), i get the message that "iTunes.exe has preformed an illegal operation and will be closed".
    Though I just noticed that if I disconnect the iPod before closing itunes, it closes fine and normally.
    I Have tried to solve it by following toturials for uninstalling both just iTunes, and iTunes + QuickTime at the same time, and preform separate installations after cleaning the temp. folder and restarting. None of it has helped me solve the problem
    -And I have lived with ifor a while, but it is getting really annoying if I am sititng on my computer and jsut close iTunes, without disonnecting my iPod, to run another program using sound or a lot of system resources. And thne each time get the error-message that "iTunes preformed an illegal operation and..."
    Anytihng that can be done? -I run NO plugins for the record, and the iPod is connected by USB cable (seeing it is a Nano)
    Anything anyne can suggest that I'd do, then please tell.
    -Or if anyone need to know more then please ask.
    Bigtower III Pentium 4   Windows XP Pro   [4gb Ipod nano. 2nd. gen.][Itunes 7.0.2.16]
    Bigtower III Pentium 4   Windows XP Pro  

    Hi, Im sorry just to add on to this discussion but I couldnt figure out how to start a new discussion and I am going mad! I have a new ipod because my last one was stolen and I have plugged it in and my PC wont recognise it. I think that is because my old ipod is the only one my computer will accept. Can that be right? It said the hardware device had been detected and then it said that it was troubled and it simply did not work. Now it is plugged in and not at all recognised in my computer anywhere. Can any one help this clueless girl who simply wants to listen to music again?
    Thank you.

  • Time Machine works on Ethernet connection but not wireless

    Hi,
    This weekend I setup time machine to work with a NAS drive connected to my wireless router. If I connect my macbook to the router directly via an ethernet cable, and start a backup, it works fine. If I however try to start a backup when only on wireless it always fails. Here's what messages I'm getting:
    Starting standard backup
    Attempting to mount network destination using URL: afp://;AUTH=No%20User%20Authent@Backup%20to%20Iomega-0cd217.afpovertcp.tcp.local/backups
    NetAuthConnectToServerSync failed with error: 64 for url: afp://;AUTH=No%20User%20Authent@Backup%20to%20Iomega-0cd217.afpovertcp.tcp.local/backups
    Attempting to mount network destination using URL: afp://;AUTH=No%20User%[email protected]/backups
    Mounted network destination using URL: afp://;AUTH=No%20User%[email protected]/backups
    Warning: Destination /Volumes/backups does not support TM Lock Stealing
    Warning: Destination /Volumes/backups does not support Server Reply Cache
    QUICKCHECK ONLY; FILESYSTEM CLEAN
    Disk image /Volumes/backups/Garys-MacBook-Pro_xxxxxxxxxxxx.sparsebundle mounted at: /Volumes/MAC Backups
    Backing up to: /Volumes/MAC Backups/Backups.backupdb
    Stopping backupd to allow ejection of backup destination disk!
    Error: (5) getxattr for key:com.apple.backupd.SnapshotState path:/Volumes/MAC Backups/Backups.backupdb/Gary's MacBook Pro/2011-01-15-042811
    Error: (5) getxattr for key:com.apple.backupd.SnapshotContainer path:/Volumes/MAC Backups/Backups.backupdb/Gary's MacBook Pro/2011-01-15-042811
    Error: (5) getxattr for key:com.apple.backupd.SnapshotState path:/Volumes/MAC Backups/Backups.backupdb/Gary's MacBook Pro/2011-01-15-171318.inProgress
    Error: (5) getxattr for key:com.apple.backupd.SnapshotContainer path:/Volumes/MAC Backups/Backups.backupdb/Gary’s MacBook Pro/2011-01-15-171318.inProgress
    Error: (5) getxattr for key:com.apple.backupd.SnapshotState path:/Volumes/MAC Backups/Backups.backupdb/Gary’s MacBook Pro/2011-01-15-042811
    Error: (5) getxattr for key:com.apple.backupd.SnapshotContainer path:/Volumes/MAC Backups/Backups.backupdb/Gary’s MacBook Pro/2011-01-15-042811
    Error: (5) getxattr for key:com.apple.backupd.SnapshotState path:/Volumes/MAC Backups/Backups.backupdb/Gary’s MacBook Pro/2011-01-15-171318.inProgress
    Error: (5) getxattr for key:com.apple.backupd.SnapshotContainer path:/Volumes/MAC Backups/Backups.backupdb/Gary’s MacBook Pro/2011-01-15-171318.inProgress
    Backup canceled.
    [SnapshotUtilities mountPointForVolumeRef] FSGetVolumeInfo returned: -35
    Failed to eject volume (null) (FSVolumeRefNum: -179; status: -35; dissenting pid: -1)
    [SnapshotUtilities mountPointForVolumeRef] FSGetVolumeInfo returned: -35
    Failed to eject volume (null) (FSVolumeRefNum: -178; status: -35; dissenting pid: -1)
    I've x'ed out the mac address from the sparsebundle name. Any help would be much appreciated as I can't understand why it's working when connected but not when on wireless.
    Thanks!

    GT777 wrote:
    Hi,
    Hi, and welcome to the forums.
    This weekend I setup time machine to work with a NAS drive connected to my wireless router.
    Time Machine doesn't work well, if at all, with many NASs. See the pink box in #2 of [Time Machine - Frequently Asked Questions|http://web.me.com/pondini/Time_Machine/FAQ.html] (or use the link in *User Tips* at the top of this forum).
    There are several threads in the Snow Leopard > Time Machine forum with what appear to be similar problems with Iomega NASs.
    If I connect my macbook to the router directly via an ethernet cable, and start a backup, it works fine. If I however try to start a backup when only on wireless it always fails.
    An indication of a bad wireless connection or interference, but could also be the setup of the NAS or compatibility with the way the router works in wireless mode.
    Mounted network destination using URL: afp://;AUTH=No%20User%[email protected]/backups
    After an initial failure, it did connect.
    Warning: Destination /Volumes/backups does not support TM Lock Stealing
    Warning: Destination /Volumes/backups does not support Server Reply Cache
    You've posted in the Leopard forum, and didn't specify what version of OSX you're on. But those messages usually indicate that your NAS is not fully compatible with the Time Machine requirements on Snow Leopard.
    Error: (5) getxattr for key:com.apple.backupd.SnapshotState path:/Volumes/MAC Backups/Backups.backupdb/Gary's MacBook Pro/2011-01-15-042811
    Those are errors reading from the backups; specifically, getting extended attributes from the backups on your NAS (a "snapshot" is Time Machine's name for a backup folder).
    First, contact the maker of the NAS for updated software/firmware compatible with Snow Leopard.
    That might fix the problem. If not, all we can tell you is, it may be some problem with the setup of the NAS, and/or compatibility with the wireless function of your router. See the maker's instructions/Help/support.

  • My usb that i have been using on my computer for a while suddenly stopped working, but is still glowing and letting me know that it is connected, but not showing up in disk utility

    my usb that i have been using on my computer for a while suddenly stopped working, but is still glowing and letting me know that it is connected, but not showing up in disk utility

    Either the physical drive or the enclosure electronics has failed. The light you are seeing is powered by the USB connection, the power, electricity, all USB ports put out. That doesn't mean the actual physical hard drive or the USB to SATA bus electronics are working properly. If it doesn't show in Disk Utility then it is DEAD.
    Most of the time it is the enclosures electronics that fail. Removing the physical drive from the enclosure and put it in another enclosure or using a SATA to USB adapter, one that has it's own AC power supply, might allow you to get the files off the drive.

  • How do I save a scan file in OS X Maverick? The file can be saved, but not readable.

    How do I save a scan file in OS X Maverick properly? The file can be saved, but not readable.

    I've never had a problem with that. What does the error message say when you try to open it?

  • I received a replacement iPad. On my original iPad I had dozens of books and PDF's. I completely backed up my original iPad. The categories of my iBook files transferred but NOT the actual PDF and Books. Please help.

    I received a replacement iPad. On my original iPad I had dozens of books and PDF's. I completely backed up my original iPad. The categories of my iBook files transferred but NOT the actual PDF and Books. Please help.

    Are the PDFs and ibooks on your computer's iTunes ? If so then you should be able to re-sync them, if not then you can re-download ibooks for free as long as they are still available in the store and you use the same iTunes account as you originally used to buy them : http://support.apple.com/kb/HT2519 . The backup doesn't contain the contents of the ibooks app, so for them to be restored to the device they needed to be in your iTunes library.

  • TS1717 What do I have to do to keep the iTUNES program on my computer BUT NOT RUNNING on Start UP? It requires too much CPU usage on my old computer.

    What do I have to do to keep the iTUNES loaded as a program on my computer BUT NOT RUNNING on Start UP? Just opening when I want to use it.
    It requires too much CPU usage on my old computer.

    You can check you Adobe account online to see if the serial number is registered under your account.  If not then your only option will likely be to contact Adobe Support directly thru chat to see if they can provide you with a/the serial number. For them to do so will require you being able to prove ownership.
    Serial number and activation chat support (non-CC)
    http://helpx.adobe.com/x-productkb/global/service1.html ( http://adobe.ly/1aYjbSC )

  • My 5th gen nano will not sync win 7 .says connected but not show in itunes. driver is updated ,working properly ....help

    my 5th gen nano ,has updated driver ,working properly. ipod connected but not showing in itunes ?

    By the way, I totally restored everything. It was ok. It starts to 'unrecognize' my iPod Classic when it comes to synching with iPhoto. After this happened all the menus disappeared from my iPod (music, Podcasts, movies synched fine). It seems to be a problem when synching with iPhoto. It was the same thing before.

  • E4200 Mac will connect but not pc after power outage

    We had a power outage.  Wireless connection disrupted and did not correct itself as in the past.
    A MAC and an android os phone will connect but not PC's with different operating systems.
    Hardwired computers will connect and go on the internet but not when try wirelessly on windows PC's.
    I have a comcast modem and a e4200 wireless router.
    Tried repowering modem and router.
    Tried disconnecting Cisco on computers and then reconnecting to wireless connection.
    Sometimes laptops will discover the Cisco and show excellent strength of signal but will not connect.
    Wireless is active on the laptops.
    Not sure what has changed to cause this for the win PC's only.  (IP?etc?)
    Any help is appreciated.
    Thanks.
    Solved!
    Go to Solution.

    I read thru some article about the E4200
    Reset
    This button allows you to reset the router to its factory defaults.  Press and hold the Reset button for approximately 10 seconds.  This button is located between the USB and Power ports.  As an alternative, you can restore the defaults from the Administration tab > Factory Defaults sub-tab in the router’s web-based setup page.
    Title: Getting to know the Linksys E4200
    Article ID: 22737
    http://www6.nohold.net/Cisco2/ukp.aspx?vw=1&docid=7824947fe4444ac3ad5ccb896007e9b8_Getting_to_Know_t...

  • Got a new LG in Anchorage and was told Glennallen, AK is in a service area.  Got home and all I get is Message 41, Roaming. Can make calls with WiFi connection but not on the road. Any idea whats going on?

    Got a new LG in Anchorage and was told Glennallen, AK is in a service area.  Got home and all I get is Message 41, Roaming.
    I can make calls with WiFi connection but not on the road. Any idea whats going on?

        Oh no! I'm sorry to hear you had problems in the are you're in AkJohnDye. I checked your coverage and I do see we have an extended 4G coverage. This means you're able to use your phone but it needs to be activated in a 4G LTE Verizon Wireless coverage zone. I apologize.
    AdaS_VZW
    Follow us on Twitter at @VZWSupport 

  • On 3G we cannot send emails from either our iPhones or iPad and this also happens with some WiFi connections but not all.  Yet we can always send emails from our Hotmail Email account.  What is causing this and what do we need to do to resolve it?

    On 3G we cannot send  Business emails from either our iPhones or Ipad and this also happens with some WiFi connections but not all.  Yet we can always send emails from our Hotmail Account using both 3G and WiFi.
    We bought the iPhones and Ipad so that we could send emails while we are out of the Office, but we are not able to do this unless we can find a WiFi connection. Incoming emails are fine.  We use IMAP, for Business emails just incase this is relevant and I know that Hotmail is POP3.
    Our technical IT knowledge is not great, so we look forward to your suggestions as to how to resolve this. 

    Contact whomever supports the email account and get the correct Outgoing email server settings from them.

  • Cisco vpn 5.0.07.0440-k9 connected but not access remote network from Windows 8.1 pro

    I am using Cisco vpn 5.0.07.0440-k9 and Cisco vpn 5.0.07.0290-k9 both version on our windows 8.1 pro laptop.
    VPN successfully connected but not access remote network and not getting ping. 
    But when i am try through wifi then vpn good work.
    Please help me as soon as possible.
    Thanks
    Sanjib

    Hello Karthik,
    I am using "MTS usb wifi" device and connect vpn through wifi Its working good the same win8.1 pro. But when i am try to connect VPN through LAN/Wired or USB modem (Like: Vodafone,MTS and others) its not working.
    I am using Easy vpn on Cisco RV325 router in our office. Same VPN client is very good working in Windows 7 SP1 and Windows XP SP3.
    Thanks
    Sanjib

  • HT1661 My mid-2009 MacBook Pro has just died via a painful (for me) hard drive failure. I backed up a good percentage of files recently but not enough to keep me happy.  I need to access the dead drive on the MBP via a 2008 iMac. Best options?

    My mid-2009 MacBook Pro has just died via a painful (for me) hard drive failure. I backed up a good percentage of files recently but not enough to keep me happy.  I need to access the dead drive on the MBP via a 2008 iMac. Best options?
    I have ordered a 800 firewire to 800 firewire cable and am hoping to use the target mode option to copy files from the MBP.
    Is this possible?
    Hope so!
    Any advice and or help would be greatly appreciated.
    Many thanks,
    Neil

    Thanks Ogelthorpe.
    Just bought an enclosure to try that also.
    Hoping the MBP isn't really dead but just feigning death
    Fingers crossed.

Maybe you are looking for

  • Office Web Apps Server 2013 issue

    Dear All, I am encounter problem when I try to open document from my browser. Prompt message will pop-out when it try to preview a document: "sorry, there was a problem and we can't open this document. If this happens again, try opening the document

  • Reg: Error in Getting Wrong output using concurrent request

    Hi all, I created a rdf using oracle 10g report for below query SELECT EMPNO,ENAME,DEPTNO,SAL,MGR FROM EMP I Registered above created rdf in oracle apps with output format as xml when i ran the report using concurrent request, i am getting below outp

  • Move recorded document between scripts using session map

    Hi all experts on scripting! We have a 2 scripts ! First script is a callback script that is recording a message from the calling customer which is saved in a"recorded document" that will be played towards an Agent when he/she is picking up the callb

  • Very weird 64 bit problem

    Initially I was running 2.4 in 64 bit mode on my Mac. Everything worked perfectly for a week or so. Then today, after messing about in the Web Module, I am not able to type in text fields. No characters appear even though if I'm outside of LR the key

  • ZEN TOUCH FM TRANSMIT

    Hello . I really need FM Transmitter for my Zen TOuch to put it into my car audio system ......i DONT HAVE CASSETE i have CD-PLAYER so i cant connect the Cassete adapter......the only way is the FM Transmitter....any suggestions ? which one ? any sit