This discussion is archived
0 Replies Latest reply: Apr 19, 2012 7:33 PM by 931961 RSS

HttpURLConnection issue reading a gzip application/json inputstream

931961 Newbie
Currently Being Moderated
while trying to send from a java proxy server this link, to browser, I get "Uncaught SyntaxError: Unexpected end of input"

when I remove "description" property from json, I get no error at all.

bypassing java server, or using "wget" from command line, i get no error too.

*******
my code
**********

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author giacomo
*/
public class GooHan implements HttpHandler {
@Override
public void handle(HttpExchange he) throws IOException {
byte[] bytin=null;
String req=null;
if(he.getRequestMethod().equalsIgnoreCase("POST")){

int lun=0;
try{
lun= Integer.parseInt(he.getRequestHeaders()
.getFirst("Content-length"),10);
}catch (NumberFormatException e) {
lun=0;
}
if(lun>0){

InputStream in=he.getRequestBody();
bytin=new byte[lun];
in.read(bytin);

req=new String(bytin,java.nio.charset.StandardCharsets.UTF_8);
/*validation problem*/
byte[] bufou=null;
HttpURLConnection conn =null;
try{
conn=(HttpURLConnection)(new URL(req).openConnection());
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept",he.getRequestHeaders().getFirst("Accept"));
conn.setRequestProperty("Accept-Charset",he.getRequestHeaders().getFirst("Accept-Charset"));
conn.setRequestProperty("Accept-Encoding",he.getRequestHeaders().getFirst("Accept-Encoding"));
conn.setRequestProperty("Cache-Control","no-cache");
conn.setRequestProperty("Connection",he.getRequestHeaders().getFirst("Connection"));
conn.setRequestProperty("Host",he.getRequestHeaders().getFirst("Host"));
conn.setRequestProperty("Referer",he.getRequestHeaders().getFirst("Referer"));
conn.setRequestProperty("User-Agent",he.getRequestHeaders().getFirst("User-Agent"));
conn.connect();
InputStream instr=conn.getInputStream();
bufou=new byte[conn.getContentLength()];
System.out.println(conn.getContentEncoding()+""+conn.getContentType());
instr.read(bufou);
he.getResponseHeaders().set("content-encoding", conn.getContentEncoding());
he.getResponseHeaders().set("content-type", conn.getContentType());
he.sendResponseHeaders(conn.getResponseCode(),conn.getContentLength());
he.getResponseBody().write(bufou,0,conn.getContentLength());
}catch (MalformedURLException e){
he.sendResponseHeaders(HttpURLConnection.HTTP_NOT_ACCEPTABLE, -1);
}
he.getResponseBody().close();
return;
}
}
he.sendResponseHeaders(HttpURLConnection.HTTP_NOT_MODIFIED,-1);
he.getResponseBody().close();
}
}

***********
main class
***********
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author giacomo
*/
public class ExLibris {

public static final Object obj=new Object();


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ThreadPoolExecutor tpe=new ThreadPoolExecutor(4, 6, 360,
TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
tpe.prestartAllCoreThreads();
try {
HttpServer serv=HttpServer.create(new InetSocketAddress(8000),15);
serv.createContext("/gooque",new GooHan());
serv.setExecutor(tpe);
serv.start();
synchronized(obj){
try {
obj.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ExLibris.class.getName()).log(Level.SEVERE, null, ex);
}
}
serv.stop(720);
tpe.shutdown();
try {
tpe.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
Logger.getLogger(ExLibris.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(ExLibris.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

any idea?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points