How to implement a persistent HTTP connection on client side...

Sorry for the inconvenience everyone, but I could really use some help here since I am a total newbie to Java programming
I am trying to implement a persistent HTTP connection on the client side with request pipelining. The way I understand it is that client requests will be sent to the server, which will reply to them back to back, and then the client will read them accordingly. But when I read the server responses, I get only the reply for the first request, and nothing further, when I actually expect it to give me as many responses as the number of requests I ussued. Here is a sample code illustrating what I am trying to do.
====================
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpClient {
public static String host = "www.somehost.com";
public static int port = 80;
public static String pathname = "/download.asp";
public static String protocol = "http";
public static ArrayList list;
public static void main(String[] args) {
Parser parser = new Parser();
list = parser.getIDs();
try {
Socket socket = new Socket(host, port);
InputStream from_server = socket.getInputStream();
PrintWriter to_server = new PrintWriter(socket.getOutputStream());
OutputStream to_file;
to_file = new FileOutputStream("HTTP_response.txt", true);
for(int i = 0; i < 3; i++)
String s = (String)parser.getIDs().get(i);
String filename = "/download.asp?id=" + s;
to_server.print("GET " + filename + "\n\n");
to_server.flush(); // Send it right now!
//This response gives only  only the response to the first
//request,....
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from_server.read(buffer)) != -1)
to_file.write(buffer, 0, bytes_read);
to_file.close();
socket.close();
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java HttpClient <URL> [<filename>]");
}

There are many things wrong with your code. Learn the HTTP protocol and you'll know what I mean.
Here's a hint - you'r missing headers in your code.
Some servers do not support keep-alive connections, nor do you request a keep-alive connection.
Connection: keep-alive
Use that header field and see what happends. Also, in your while-loop, you actually close the output file and the socket. Don't you think it may be hard for the server to respond to a client that closed the connection on it?
1. You don't know HTTP
2. You don't seem to know Java
3. You don't seem to understand basic programming logic

Similar Messages

  • It appears that Adobe Air does not support persistent HTTP connections?

    As far as we can tell, Adobe Air does not support persistent HTTP connections via KEEPALIVE
    Thus a new connection has to be established to the server, making certain types of network intensive applications slow.
    Can anyone confirm if this is correct - is there any way to get Adobe Air to support KEEPALIVE and maintain persistent server connections?
    a

    I am trying to get 3.3 Beta 2 with Flash Builder 4.6 on Windows 7 x64bit to work. But still no luck.
    I have Flash Builder 4.6 on Win7 to work no problem.
    I have updated the playerglobal.swc to 11.3 using http://labsdownload.adobe.com/pub/labs/flashplatformruntimes/flashplay er11-3/flashplayer11-3_p2_playerglobal_041812.swc
    but still run into the same problem below:
    I can't go through the steps to create a new Flex Mobile Project because of the following error "Air 3.3 Beta does not support mobile projects".
    However, I have no problem creating a Flex Project (Web & AIR) and ActionScript Mobile Project.

  • How to get and put data in HTTP header in Client Side

    One JSP's function is to display data records page by page. Client users could click "First page" or "Previous Page" or "Next Page" or "Last Page" button to let the JSP to show the corresponding page. So the JSP must remember the current page number. Due to there are a number of client users maybe access the JSP at the same time, keep the current page number in session, there will be a lot of session is created at the same time, this will impact system's performance. So using session to keep data method is not used. I plan to use request header and response header to pass the current page number. I know use the response.addHeader to put data in response header and also know use request.getHeader to get data in request header in server side, but I donot know how to put data in request header and how to get data in response header in client side. Could you please give me a help? If donot use these method, are there any other method? Thanks a lot.

    Why not pass it as a parameter with the URL?
    at the beginning of the page..
    <%
    int pageNumber = Integer.parseInt(request.getParameter("page"));
    %>
    then when defining your links
    <a href="thisPage.jsp?page=<%= (pageNumber-1)%>">Previous</a>
    <a href="thisPage.jsp?page=<%= (pageNumber+1)%>">Next</a>

  • Its urgent...How to implement functions in http request handler ?

    Hello experts,
    I have created http request handler class and  also created service in SICF.
    service and handler class are connected. please guide me how to implement “info”, “get”, “create”, “delete” functions in request handler.

    Hi,
    It seems you go on your way of http basic programming where you need handle the request of info, get, etc. But here you have WDA framework, so you don't need do as the basic ways of http programming. SICF can handle the activated WDA applications, maybe you can call them SERVICES.
    I suggest you go through WDA architecture, tutorials, ... and at last you can find a full demo 'LORD_MAINTAIN_COMP' by SE80.

  • How to solve Error in http connection. Error -1

    urlMidlet2===http://localhost:8080/testProj?userId=20&selMPBL=23&svMPBL=IPOD+Touch+Screen+e!1!Tue+Jan+06+00:00:00+UTC+2009!Tue+Jan+06+00:00:00+UTC+2009!Sat+Jan+31+00:00:00+UTC+2009!Mp3+Player!IPOD!Rejected!12
    [j2mepolish] Generic/DefaultColorPhone: !1!2!1222!13333
    [j2mepolish] Generic/DefaultColorPhone: Protocol for this connection is http
    [j2mepolish] Generic/DefaultColorPhone: This object is connected to localhost host
    [j2mepolish] Generic/DefaultColorPhone: HTTP Port in use is 8080
    [j2mepolish] Generic/DefaultColorPhone: respCode=====-1
    [j2mepolish] Generic/DefaultColorPhone: Error in opening HTTP Connection. Error#-1
    Why is this error coming? How to solve it?
    Thanks in advance.
    Edited by: J2me_Beginner on Jun 7, 2009 9:38 PM

    I am making a form using an interface. When Activate the Form, I got this error:
    Invalid HTTP connection: ADS
    Message no. FPRUN000
    But I do not know how to solve or config ADS. Any one know how to slove this error ?
    Thanks a lot.

  • How  to create and test HTTP connection for proxy

    HI
    Can anybody help me by providing step by step guide for how to create a HTTP connection for inbound abap proxy?
    How i will get sure that connection is working properly?
    Thanks
    Debraj

    Hi Debraj,
       You should be able to do that in SM59 of the XI Box.
    Create the connection under "HTTP Connections to R/3 System".
    Provide the Target Host IP Address.Service Number and Path Prefix.
    Select the logon procedure as SAP Standard.
    SSL Inactive.
    Provide Logon details.
    Regards,
    Ravi

  • Configuring Sender HTTPS Connection -- Server/Client Authentification

    Hello together,
    I need to configure an HTTPS Sender Connection with client and server authentication. I have already check the documentation however I am still not sure about the particular steps. My questions are as follows:
    - Do I configure the HTTPS connection on the ABAP or JAVA stack?
    - Is it necessary to setup an HTTP sender communication channel
    - How does the URL look like (compared to HTTP connection)?
    I have provided XI certificates to the client and the client has provided the certificates to me already. So I guess I have to import them somehow on XI.
    Any help is appreciated!
    Thank you very much.

    Hi
    Please follow below steps for HTTPS configuration as sender
    You need to use either SOAP adapter or XI Adapter for HTTPS connectivity.
    Here configure the Security Check for Inbound Messages.
    Refer below links
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/5ad93f130f9215e10000000a155106/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/4f/0a1640a991c742e10000000a1550b0/frameset.htm
    XI3.0: Soap Sender with HTTPS
    SAP Security Guide XI, HTTP and SSL
    http://help.sap.com/saphelp_nw04/helpdata/en/14/ef2940cbf2195de10000000a1550b0/content.htm
    http://help.sap.com/saphelp_nw04s/helpdata/en/97/818a4286031253e10000000a155106/frameset.htm
    No configuration is required in the adapter-specific sender channel configuration (inbound) of the Integration Directory.
    The authentication/authorization is performed by the J2EE Engine and therefore needs to be configured with the Visual Administrator. This configuration is described in the J2EE Engine Administration Manual and is outlined in the following section.
    When a message is to be sent to the Adapter Engine (and ultimately to the Integration Server), the J2EE Engine serves as the SSL Server and presents its server certificate to the client as part of the SSL handshake procedure.
    Client-Side Configuration (Required)
    The public certificate of the trusted authority (CA) that signed the public certificate of the SSL server needs to be imported to the list of trusted certificates of the SSL client. This allows the SSL client to accept the certificate of the server in the SSL handshake.
    Server-Side Configuration (Optional)
    If basic authentication is used, no additional configuration is required on server side.
    If client certificate authentication is requested or required by selection of the corresponding option in the SSL service and configuration of the ClientCertLoginModule in the SecurityProvider service (using the J2EE Administration Tool), additional configuration steps are required.
    If the server certificate check on the client side is successful, the client sends its public certificate to the server as part of the SSL handshake (when requested). The server needs to map the certificate to a user for authentication and will then check the authorization based on the security roles of the user.
    Perform the following steps to allow the J2EE engine to map the client certificate to a user:
           1.      Import the CA cert of the client certificate to the list of trusted certificates (TrustedCAs keystore view in the keystore service) and import the client cert to an arbitrary keystore view.
           2.      Map the client certificate to an existing user with role SAP_XI_APPL_SERV_USER by using the Visual Administrator, SecurityProvider service, UserManagement tab page.
    Refer below link
    Here u go
    http://help.sap.com/saphelp_nw04/helpdata/en/65/6a563cef658a06e10000000a11405a/content.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/f1/2de3be0382df45a398d3f9fb86a36a/frameset.htm

  • How can i load file into database from client-side to server-side

    i want to upload file from client-side to server-side, i use the following code to load blob into database.
    if the file is in the server-side, it can work, but if it in the client-side, it said that the system cannot find the file. i think it only will search the file is in the server-side or not, it will not search the client-side.
    how can i solve it without upload the file to the server first, then load it into database??
    try
    ResultSet rset = null;
    PreparedStatement pstmt =
    conn.prepareStatement ("insert into docs values (? , EMPTY_BLOB())");
    pstmt.setInt (1, docId);
    pstmt.execute ();
    // Open the destination blob:
    pstmt.setInt (1, docId);
    rset = pstmt.executeQuery (
    "SELECT content FROM docs WHERE id = ? FOR UPDATE");
    BLOB dest_lob = null;
    if (rset.next()) {
    dest_lob = ((OracleResultSet)rset).getBLOB (1);
    // Declare a file handler for the input file
    File binaryFile = new File (fileName);
    FileInputStream istream = new FileInputStream (binaryFile);
    // Create an OutputStram object to write the BLOB as a stream
    OutputStream ostream = dest_lob.getBinaryOutputStream();
    // Create a tempory buffer
    byte[] buffer = new byte[1024];
    int length = 0;
    // Use the read() method to read the file to the byte
    // array buffer, then use the write() method to write it to
    // the BLOB.
    while ((length = istream.read(buffer)) != -1)
    ostream.write(buffer, 0, length);
    pstmt.close();
    // Close all streams and file handles:
    istream.close();
    ostream.flush();
    ostream.close();
    //dest_lob.close();
    // Commit the transaction:
    conn.commit();
    conn.close();
    } catch (SQLException e) {

    Hi,
    Without some more details of the configuration, its difficult to know
    what's happening here. For example, what do you mean by client side
    and server side, and where are you running the upload Java application?
    If you always run the application on the database server system, but can't
    open the file on a different machine, then it sounds like a file protection
    problem that isn't really connected with the database at all. That is to
    say, if the new FileInputStream (binaryFile) statement fails, then its not
    really a database problem, but a file protection issue. On the other hand,
    I can't explain what's happening if you run the program on the same machine
    as the document file (client machine), but you can't write the data to the
    server, assuming the JDBC connection string is set correctly to connect to
    the appropriate database server.
    If you can provide some more information, we'll try to help.
    Simon
    null

  • How to use javascript to print on the client side

    hi all,
    Now i'm facing a problem:
    I want to use javascript to print something(for example, a table) on the client side. So i must get a connection with the printer installed in the client computer.
    some says that getObject() function can do this,but i don't know how?
    Who please help me!

    window.print();

  • How to read and write Xml file at client side using JavaScript !

    Hello,
    i am new to javascript.
    I have requirement to read and update XML file at client side.
    Will you please guide what could be the best way to read and update XMl file using javascript.
    Thanks,
    Zuned

    This is a Java forum,not a Javascript forum. Maybe you should ask here [http://www.webdeveloper.com/forum/forumdisplay.php?forumid=3&s|http://www.webdeveloper.com/forum/forumdisplay.php?forumid=3&s].

  • How to read from untrusted https connection?

    i try to read the content of a https URL but when calling httpsurl.getContent() or con.getInputStream() i get the following exception:
    javax.net.ssl.SSLException: untrusted server cert chain
         at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
         at com.sun.net.ssl.internal.ssl.ClientHandshaker.a(DashoA6275)
         at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(DashoA6275)
         at com.sun.net.ssl.internal.ssl.Handshaker.process_record(DashoA6275)
         at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
         at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
         at com.sun.net.ssl.internal.ssl.AppOutputStream.write(DashoA6275)
         at java.io.OutputStream.write(OutputStream.java:56)
         at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsClient.doConnect(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.NetworkClient.openServer(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpClient.l(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpClient.<init>(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsClient.<init>(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsClient.a(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsClient.a(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnection.connect(DashoA6275)
         at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnection.getInputStream(DashoA6275)
    the same https url works in internet explorer although a little dialog appears that informs about the untrusted certificate, but i can click O.K. and get the content. How can i tell my java application that it should read the content of such an untrusted server?

    I am recyling this for OTT purposes..
    Continuing on 360-degree feedback, here is a managerial take on it:
    Q: What is a 360-degree feedback?
    Manager: You give me open and honest opinions about my performance. Then I take it all on board and do absolutely nothing. If it but worketh, thou may even be introduced to a concept called wages.

  • Can AMS establish persistent https connection?

    I am wondering if AMS is able to make a connection through https that can remain persistent. Are there any gotchas or special considerations to using https as opposed to http? We want to switch to https, but due to the overhead in the handshake, want to keep a persistent connection to send data at random times.

    So from what I can tell, FMS just isn't set up to do this. It looks like if you want security they want you to use rtmps through ssl. There may be some other ways to do it, but https doesn't seem to be officially supported and I can't find anyone able to do it successfully.
    The WebServices class definitely can't do it. I tried the LoadVars class, which works great on an http page, but fails to do anything with https. It won't even throw an error.
    here's a few random pages I looked through, though none of them give any solid info:
    http://forums.adobe.com/thread/773705 - says it's possible to flash but the config xml stuff they're talking about seems to be just for rtmps, not https
    http://forums.adobe.com/message/3704669 - also says it's possible to do dual connections, one https, but looks like they may have meant rtmps instead of https
    these guys seem to be saying it's not possible:
    http://stackoverflow.com/questions/5619776/webservice-and-fms 
    //seems to think it can be done, but again, looks like they're talking about ssl over rtmp
    http://nycphotoshop.groups.adobe.com/index.cfm?event=post.display&postid=5194
    someone trying to make it work but never does:
    http://forums.adobe.com/thread/782037?decorator=print&displayFullThread=true

  • How do I change the http connection type from close to keep-alive

    I am using a browser that appears as though it needs a connection type of keep-alive. When a page is requested the server sends back a connection type of close. It appears images are not requested from the server when this connection type is requested.

    Some older web server provides inaccurate content-length information.If the content length value is less than the amount of data,the web server treats the difference as a new request, this creates problem with iplanet Web Server.
    If you are using browser with HTTP1.1 enabled, choose the option to enable it manually and try once again by posting the request.
    Hope it helps.

  • Proxy https connection with client certificate credentials

    Hello, we are building a application like netvibes/iGoogle which allows users to have portlets with rss feeds in them. The portlets are all loaded using ajax and therefore, the RSS feeds must exist on the same domain as the portal. If they don't, you run into problems with cross-domain security issues with ajax. Usually to get around this you just proxy the connection on the server which is very simple with rss feeds that are exposed via http. We however have many feeds that are exposed via https. These feeds likely require a client certificate to authenticate them. Therefore, just doing a basic proxy (take the distant url and open a new connection on the server) won't work because it will build the new connection with the servers credentials and not the users.
    Is there a way to build the connection on the server using the users credentials?? How can we proxy this connection over https?
    If anyone has ideas, please let me know.
    Thanks!

    in fact you are more using a reverse-proxy than a proxy since it is on the server part..
    You have to put all the SSL server part on the reserve-proxy itself and not on the final RSS feed. Then, the reverse-proxy will authenticate your client and gets its certificate. After that, either this proxy will open a plain connection (no ssl) towards the RSS, or you can also open a ssl connection but this means you must create a client certificate for the proxy. It just depends on the security level you need, and I used this solution many times in professional hosting.
    hope it helps !

  • HTTPS connection with client certificate not working in spartan

    Spartan does not show certificate for the user to select
    when I click the https link.
    The certificates (taken from a smartcard) are indeed present in the user CertStore.
    It works with IE 11 and Chrome.
    Has somebody any suggestions ?
    Thanks.

    in fact you are more using a reverse-proxy than a proxy since it is on the server part..
    You have to put all the SSL server part on the reserve-proxy itself and not on the final RSS feed. Then, the reverse-proxy will authenticate your client and gets its certificate. After that, either this proxy will open a plain connection (no ssl) towards the RSS, or you can also open a ssl connection but this means you must create a client certificate for the proxy. It just depends on the security level you need, and I used this solution many times in professional hosting.
    hope it helps !

Maybe you are looking for

  • How can I store an output, i.e C from the attatchmen​t in an array?

    How can I store an output, i.e C from the attatchment in an array? Attachments: Testing.vi ‏18 KB

  • Maximun length of name

    Hello Gurus,         will you please telll the maximun length of name for following object in SAP BW? Datasource ,  Extractor, Extract structure ,  Infoprovider /infoobject/OHS, process chain Technical name, process chain Description, View , process

  • Carry forward of variable value in cross-tab charts

    Hi Experts, I have a cross-tab which calculates the inventory of items (based on count). For a particular date range (quarterly here), if i have to generate the cross-tab, i set the inventory count to zero for the first quarter, and from the next qua

  • Bar code in Smartforms

    Hi experts can u plz explain me how to bring bar code in to smartform like invoice  credit notes step by step procedure regards Siri

  • N8 Headphones (still)

    N8 Headphones buttons do not work with these methods - Inserting fast (It used to work but stopped suddenly; I use them everyday) - Inserting fully til you hear the click sound etc.. - Switching the default mode in accessories - Inserting with music