You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by corpaul <ko...@live.nl> on 2008/11/26 16:45:26 UTC

Question about creating a simple HTTP proxy

Hi,

I am trying to adjust the ReverseProxy example to build a (simple) HTTP
proxy. I need one to hook up to another Java application to see whether a
request was made and to which Host it was made, just for testing purposes.
My desire is not to build a production quality proxy (although it would be
nice if it worked correctly ;) ).

I have a couple of questions about this:

1. Do such proxies exist for HttpClient / Java? I kinda have the feeling I'm
reinventing the wheel here.
2. Is adjusting the ReverseProxy example a good idea or is this way too
naive?
3. The code I have thus far gives me the following error for AJAX calls:
Exception in thread "Thread-6" java.lang.IllegalStateException: Content has
been consumed
	at
org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
	at org.apache.http.entity.BasicHttpEntity.writeTo(BasicHttpEntity.java:126)
	at
org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
	at
org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:164)
	at
org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
	at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119)
	at usaproxytest.NHttpServer$HttpFileHandler.handle(NHttpServer.java:178)
	at org.apache.http.protocol.HttpService.doService(HttpService.java:243)
	at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
	at usaproxytest.NHttpServer$WorkerThread.run(NHttpServer.java:272)
What does this imply?

Thanks in advance for your time!

Cor-Paul
-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20703487.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Question about creating a simple HTTP proxy

Posted by sebb <se...@gmail.com>.
On 26/11/2008, Chris Lowe <ch...@triggersoft.com> wrote:
> If it's just for debugging then I strongly recommend using the Charles
>  Web Debugging Proxy:
>
>  http://www.charlesproxy.com/

http://wiki.apache.org/jakarta-jmeter/NetworkSniffer

has some other suggestions.

>
>  If your Java programs are using JDK APIs then you can specify the
>  following VM argument "-Djava.net.useSystemProxies=true" and you should
>  see all traffic go through Charles.
>
>  http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/pr
>  oxie_config.html

I doubt that will apply to HttpClient code as it uses sockets directly
rather than using UrlConnection.

>  Cheers,
>
>
>  C.
>
>
>
>
>  -----Original Message-----
>  From: corpaul [mailto:koningcor@live.nl]
>  Sent: 26 November 2008 15:45
>  To: httpclient-users@hc.apache.org
>  Subject: Question about creating a simple HTTP proxy
>
>
>  Hi,
>
>  I am trying to adjust the ReverseProxy example to build a (simple) HTTP
>  proxy. I need one to hook up to another Java application to see whether
>  a request was made and to which Host it was made, just for testing
>  purposes.
>  My desire is not to build a production quality proxy (although it would
>  be nice if it worked correctly ;) ).
>
>  I have a couple of questions about this:
>
>  1. Do such proxies exist for HttpClient / Java? I kinda have the feeling
>  I'm reinventing the wheel here.
>  2. Is adjusting the ReverseProxy example a good idea or is this way too
>  naive?
>  3. The code I have thus far gives me the following error for AJAX calls:
>  Exception in thread "Thread-6" java.lang.IllegalStateException: Content
>  has been consumed
>         at
>  org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:8
>  4)
>         at
>  org.apache.http.entity.BasicHttpEntity.writeTo(BasicHttpEntity.java:126)
>         at
>  org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.
>  java:97)
>         at
>  org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(Abst
>  ractHttpClientConnection.java:164)
>         at
>  org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestEx
>  ecutor.java:237)
>         at
>  org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor
>  .java:119)
>         at
>  usaproxytest.NHttpServer$HttpFileHandler.handle(NHttpServer.java:178)
>         at
>  org.apache.http.protocol.HttpService.doService(HttpService.java:243)
>         at
>  org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
>         at
>  usaproxytest.NHttpServer$WorkerThread.run(NHttpServer.java:272)
>  What does this imply?
>
>  Thanks in advance for your time!
>
>  Cor-Paul
>  --
>  View this message in context:
>  http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp2070
>  3487p20703487.html
>  Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Question about creating a simple HTTP proxy

Posted by corpaul <ko...@live.nl>.
I will give this one a try! Thanks!


sebb-2-2 wrote:
> 
> In that case, Apache TCPMon
> 
> http://ws.apache.org/commons/tcpmon/tcpmontutorial.html
> 
> is written in Java.
> 

-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20704319.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Question about creating a simple HTTP proxy

Posted by sebb <se...@gmail.com>.
In that case, Apache TCPMon

http://ws.apache.org/commons/tcpmon/tcpmontutorial.html

is written in Java.

On 26/11/2008, corpaul <ko...@live.nl> wrote:
>
>  Hi,
>
>  thanks for your message. I know of many other existing HTTP proxies but I
>  want to be able to include it in my Java project rather than use an external
>  application because I want a way to communicate with the proxy (e.g. notify
>  when a request comes in, use the request in Java etc).
>
>  CP
>
>
>
>
>  Chris Lowe wrote:
>  >
>  > If it's just for debugging then I strongly recommend using the Charles
>  > Web Debugging Proxy:
>  >
>  > http://www.charlesproxy.com/
>  >
>  > If your Java programs are using JDK APIs then you can specify the
>  > following VM argument "-Djava.net.useSystemProxies=true" and you should
>  > see all traffic go through Charles.
>  >
>  > http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/pr
>  > oxie_config.html
>  >
>  > Cheers,
>  >
>  > C.
>  >
>
>  --
>
> View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20703888.html
>
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by corpaul <ko...@live.nl>.
Here's the source:

/*
 * $HeadURL$
 * $Revision$
 * $Date$
 *
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package usaproxytest;

import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Locale;

import org.apache.http.ConnectionClosedException;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpServerConnection;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
import org.apache.http.nio.NHttpConnection;
import org.apache.http.nio.entity.NFileEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
import org.apache.http.nio.protocol.EventListener;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.ListeningIOReactor;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;
import org.apache.http.util.EntityUtils;

public class NHttpServer {

	 public static void main(String[] args) throws Exception {
	        
	        Thread t = new RequestListenerThread(8080, null);
	        t.setDaemon(false);
	        t.start();
	    }
	    
	    static class HttpFileHandler implements HttpRequestHandler  {
	        
	        private final String docRoot;
	        
	        public HttpFileHandler(final String docRoot) {
	            super();
	            this.docRoot = docRoot;
	        }
	        
	        public void handle(
	                final HttpRequest request, 
	                final HttpResponse response,
	                final HttpContext context) throws HttpException,
IOException {
	        	String method =
request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
	            /*if (!method.equals("GET") && !method.equals("HEAD") &&
!method.equals("POST")) {
	                throw new MethodNotSupportedException(method + " method not
supported"); 
	            }*/
	            String target = request.getRequestLine().getUri();

	        	System.out.println("handling request for: " + target);
	            
	            
	            if (request instanceof HttpEntityEnclosingRequest) {
	                HttpEntity entity = ((HttpEntityEnclosingRequest)
request).getEntity();
	                byte[] entityContent = EntityUtils.toByteArray(entity);
	                System.out.println("Incoming entity content (bytes)    : "
+ entityContent.length);
	                System.out.println("repeatable: "  +
entity.isRepeatable());
	            }
	            
	            // added by CP 
	            HttpParams params = new BasicHttpParams();
	            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
	            HttpProtocolParams.setContentCharset(params, "UTF-8");
	            HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
	            HttpProtocolParams.setUseExpectContinue(params, true);
	            
	            BasicHttpProcessor httpproc = new BasicHttpProcessor();
	            // Required protocol interceptors
	            httpproc.addInterceptor(new RequestContent());
	            httpproc.addInterceptor(new RequestTargetHost());
	            // Recommended protocol interceptors
	            httpproc.addInterceptor(new RequestConnControl());
	            httpproc.addInterceptor(new RequestUserAgent());
	            httpproc.addInterceptor(new RequestExpectContinue());

            
	            HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

	            HttpContext httpContext = new BasicHttpContext(null);
	            
	            URL targetUrl = new URL(target);
	            int port = targetUrl.getPort() != -1 ? targetUrl.getPort() :
targetUrl.getDefaultPort();
	            
	            
	            HttpHost host = new HttpHost(targetUrl.getHost(), port);
	            
	            DefaultHttpClientConnection conn = new
DefaultHttpClientConnection();
	            
	            context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
	            context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

	            if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(),
host.getPort());
                    conn.bind(socket, params);

                }

	            
	            System.out.println(">> Request URI: " +
request.getRequestLine().getUri());

                context.setAttribute(ExecutionContext.HTTP_REQUEST,
request);
                
                HttpResponse httpResponse = httpexecutor.execute(request,
conn, context);
                
                response.setParams(params);

                httpexecutor.postProcess(httpResponse, httpproc, context);
                response.setStatusLine(httpResponse.getStatusLine());
                response.setEntity(httpResponse.getEntity());
                conn.close();
                                
	            // end of added by CP
	        	
	        }
	        
	    }
	    
	    static class RequestListenerThread extends Thread {

	        private final ServerSocket serversocket;
	        private final HttpParams params; 
	        private final HttpService httpService;
	        
	        public RequestListenerThread(int port, final String docroot) throws
IOException {
	            this.serversocket = new ServerSocket(port);
	            this.params = new BasicHttpParams();
	            this.params
	                .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
	                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8
* 1024)
	               
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
	                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,
true)
	                .setParameter(CoreProtocolPNames.ORIGIN_SERVER,
"HttpComponents/1.1");

	            // Set up the HTTP protocol processor
	            BasicHttpProcessor httpproc = new BasicHttpProcessor();
	            httpproc.addInterceptor(new ResponseDate());
	            httpproc.addInterceptor(new ResponseServer());
	            httpproc.addInterceptor(new ResponseContent());
	            httpproc.addInterceptor(new ResponseConnControl());
	            
	            // Set up request handlers
	            HttpRequestHandlerRegistry reqistry = new
HttpRequestHandlerRegistry();
	            reqistry.register("*", new HttpFileHandler(docroot));
	            
	            // Set up the HTTP service
	            this.httpService = new HttpService(
	                    httpproc, 
	                    new DefaultConnectionReuseStrategy(), 
	                    new DefaultHttpResponseFactory());
	            this.httpService.setParams(this.params);
	            this.httpService.setHandlerResolver(reqistry);
	        }
	        
	        public void run() {
	            System.out.println("Listening on port " +
this.serversocket.getLocalPort());
	            while (!Thread.interrupted()) {
	                try {
	                    // Set up HTTP connection
	                    Socket socket = this.serversocket.accept();
	                    DefaultHttpServerConnection conn = new
DefaultHttpServerConnection();
	                    System.out.println("Incoming connection from " +
socket.getInetAddress());
	                    conn.bind(socket, this.params);

	                    // Start worker thread
	                    Thread t = new WorkerThread(this.httpService, conn);
	                    t.setDaemon(true);
	                    t.start();
	                } catch (InterruptedIOException ex) {
	                    break;
	                } catch (IOException e) {
	                    System.err.println("I/O error initialising connection
thread: " 
	                            + e.getMessage());
	                    break;
	                }
	            }
	        }
	    }
	    
	    static class WorkerThread extends Thread {

	        private final HttpService httpservice;
	        private final HttpServerConnection conn;
	        
	        public WorkerThread(
	                final HttpService httpservice, 
	                final HttpServerConnection conn) {
	            super();
	            this.httpservice = httpservice;
	            this.conn = conn;
	        }
	        
	        public void run() {
	            System.out.println("New connection thread");
	            HttpContext context = new BasicHttpContext(null);
	            try {
	                while (!Thread.interrupted() && this.conn.isOpen()) {
	                    this.httpservice.handleRequest(this.conn, context);
	                }
	            } catch (ConnectionClosedException ex) {
	                System.err.println("Client closed connection");
	            } catch (IOException ex) {
	                System.err.println("I/O error: " + ex.getMessage());
	            } catch (HttpException ex) {
	                System.err.println("Unrecoverable HTTP protocol violation:
" + ex.getMessage());
	            } finally {
	                try {
	                    this.conn.shutdown();
	                } catch (IOException ignore) {}
	            }
	        }

	    }


}






Chris Lowe wrote:
> 
> I don't think attachments come though on this list. 
> 

-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20704331.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by Chris Lowe <ch...@triggersoft.com>.
I don't think attachments come though on this list. 

-----Original Message-----
From: corpaul [mailto:koningcor@live.nl] 
Sent: 26 November 2008 16:22
To: httpclient-users@hc.apache.org
Subject: RE: Question about creating a simple HTTP proxy


Yes, it is.

I have attached the code I have so far.

CP




Chris Lowe wrote:
> 
> Okay, fair enough.
> 
> The reverse proxy example is probably a good start then.  Is the 
> example you are referring to NHttpReverseProxy.java?
> 
http://www.nabble.com/file/p20704207/http_proxy.java http_proxy.java
--
View this message in context:
http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp2070
3487p20704207.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by corpaul <ko...@live.nl>.
Yes, it is.

I have attached the code I have so far.

CP




Chris Lowe wrote:
> 
> Okay, fair enough.
> 
> The reverse proxy example is probably a good start then.  Is the example
> you are referring to NHttpReverseProxy.java?
> 
http://www.nabble.com/file/p20704207/http_proxy.java http_proxy.java 
-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20704207.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by Chris Lowe <ch...@triggersoft.com>.
Okay, fair enough.

The reverse proxy example is probably a good start then.  Is the example
you are referring to NHttpReverseProxy.java?



-----Original Message-----
From: corpaul [mailto:koningcor@live.nl] 
Sent: 26 November 2008 16:06
To: httpclient-users@hc.apache.org
Subject: RE: Question about creating a simple HTTP proxy


Hi,

thanks for your message. I know of many other existing HTTP proxies but
I want to be able to include it in my Java project rather than use an
external application because I want a way to communicate with the proxy
(e.g. notify when a request comes in, use the request in Java etc).

CP



Chris Lowe wrote:
> 
> If it's just for debugging then I strongly recommend using the Charles

> Web Debugging Proxy:
> 
> http://www.charlesproxy.com/
> 
> If your Java programs are using JDK APIs then you can specify the 
> following VM argument "-Djava.net.useSystemProxies=true" and you 
> should see all traffic go through Charles.
> 
> http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/
> pr
> oxie_config.html
> 
> Cheers,
> 
> C.
> 

--
View this message in context:
http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp2070
3487p20703888.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by corpaul <ko...@live.nl>.
Hi,

thanks for your message. I know of many other existing HTTP proxies but I
want to be able to include it in my Java project rather than use an external
application because I want a way to communicate with the proxy (e.g. notify
when a request comes in, use the request in Java etc).

CP



Chris Lowe wrote:
> 
> If it's just for debugging then I strongly recommend using the Charles
> Web Debugging Proxy:
> 
> http://www.charlesproxy.com/
> 
> If your Java programs are using JDK APIs then you can specify the
> following VM argument "-Djava.net.useSystemProxies=true" and you should
> see all traffic go through Charles.
> 
> http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/pr
> oxie_config.html
> 
> Cheers,
> 
> C.
> 

-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20703888.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Question about creating a simple HTTP proxy

Posted by Chris Lowe <ch...@triggersoft.com>.
If it's just for debugging then I strongly recommend using the Charles
Web Debugging Proxy:

http://www.charlesproxy.com/

If your Java programs are using JDK APIs then you can specify the
following VM argument "-Djava.net.useSystemProxies=true" and you should
see all traffic go through Charles.

http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/pr
oxie_config.html

Cheers,

C.



-----Original Message-----
From: corpaul [mailto:koningcor@live.nl] 
Sent: 26 November 2008 15:45
To: httpclient-users@hc.apache.org
Subject: Question about creating a simple HTTP proxy


Hi,

I am trying to adjust the ReverseProxy example to build a (simple) HTTP
proxy. I need one to hook up to another Java application to see whether
a request was made and to which Host it was made, just for testing
purposes.
My desire is not to build a production quality proxy (although it would
be nice if it worked correctly ;) ).

I have a couple of questions about this:

1. Do such proxies exist for HttpClient / Java? I kinda have the feeling
I'm reinventing the wheel here.
2. Is adjusting the ReverseProxy example a good idea or is this way too
naive?
3. The code I have thus far gives me the following error for AJAX calls:
Exception in thread "Thread-6" java.lang.IllegalStateException: Content
has been consumed
	at
org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:8
4)
	at
org.apache.http.entity.BasicHttpEntity.writeTo(BasicHttpEntity.java:126)
	at
org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.
java:97)
	at
org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(Abst
ractHttpClientConnection.java:164)
	at
org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestEx
ecutor.java:237)
	at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor
.java:119)
	at
usaproxytest.NHttpServer$HttpFileHandler.handle(NHttpServer.java:178)
	at
org.apache.http.protocol.HttpService.doService(HttpService.java:243)
	at
org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
	at
usaproxytest.NHttpServer$WorkerThread.run(NHttpServer.java:272)
What does this imply?

Thanks in advance for your time!

Cor-Paul
--
View this message in context:
http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp2070
3487p20703487.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Question about creating a simple HTTP proxy

Posted by corpaul <ko...@live.nl>.
Thanks for your help everyone. I managed to find a Java proxy which I can use
in my code at http://maxq.tigris.org/ .

Regards, Cor-Paul


corpaul wrote:
> 
> Hi,
> 
> I am trying to adjust the ReverseProxy example to build a (simple) HTTP
> proxy. I need one to hook up to another Java application.....

-- 
View this message in context: http://www.nabble.com/Question-about-creating-a-simple-HTTP-proxy-tp20703487p20716380.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org