BillPull
BillPull

Reputation: 7013

Java Invalid HTTP response on localhost

I a writing a package for a school project where a user can specify a server url and get the xml it returns. I am getting an error though on the client side of things.

The Mock API class

import java.net.*;
import java.util.ArrayList;
import java.io.*;

public class ParkingLotInstance {
/*
 * Parking Lot API instance
 *      Constructor
 *          URL - String
 *          Port - int
 */

public static URL serverURL;

public ParkingLotInstance( URL connurl){
    serverURL = connurl;
}

public String getParkingLotInfo(){
    //Get a response from API server

    URL APIurl = this.serverURL;
    System.out.println(APIurl);
    try {
        BufferedReader in = new BufferedReader(
                new InputStreamReader(APIurl.openStream()));

        String APIresponse;
        while ((APIresponse = in.readLine()) != null)
            return APIresponse;
        in.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

public ArrayList parseParkingLotInfo( String XML ){
    //Parse XML into array list

    return null;
}
}

The Main class

package parkinglot_api;

import java.net.MalformedURLException;
import java.net.URL;


public class Example {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    URL serverURL;
    try {
        serverURL = new URL("http://localhost:8080");

        ParkingLotInstance API = new ParkingLotInstance(serverURL);

        String parkingLotXML = API.getParkingLotInfo();

        System.out.println(parkingLotXML);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

 }

The Error I am getting

http://localhost:8080
null
java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at parkinglot_api.ParkingLotInstance.getParkingLotInfo(ParkingLotInstance.java:28)
at parkinglot_api.Example.main(Example.java:20)

Server code

https://bitbucket.org/it460/parking-lot-api/src/f7095b71eb11/server

I do not know much Java especially networking stuff so above is the link to the code that runs the server on localhost:8080 and spits out the xml. Was not sure where to set the Headers and to be honest was just happy that it outputed the xml. The reason I need to adapt this is because the professor wants me to package the client side up so another group can just specify the app server url and get the data. that is what I attempted in the code posted here.

Upvotes: 3

Views: 9101

Answers (2)

Suresh Kumar
Suresh Kumar

Reputation: 11767

Include the following in your Response class:

response += "HTTP/1.1 200 OK\n";
response += "Content-Type: application/xml\n\n";

if ( format.equals("xml")){
      // Retrieve XML Document
       String xml = LotFromDB.getParkingLotXML();
       response += xml;
}

Note: You should avoid writing your own web server. Instead use an existing Web server which will help a lot. The above changes will solve your current problems but you will run into much more problems if you continue down this line. I suggest you look at using any servlet engine for you server implementation. Also, on the client use a mature HTTP client library such as Apache Http Client instead of java.net.URL class.

Upvotes: 4

Jan Arciuchiewicz
Jan Arciuchiewicz

Reputation: 791

Looks like a problem with a headers processing. Please use tcp monitor (for example: http://ws.apache.org/commons/tcpmon form apache) to find the problem. It could for example be caused by incorrect char encoding or a specific way of message multipart encoding (guessing).

Upvotes: 1

Related Questions