Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Couldn't parser response content in ProxyServlet

Dear all,
Sorry for bothering. I'm new in jetty. Now I'm working on a task, I have to change jetty ProxyServlet a little bit. So as to make it availbale for our whole project.
What I have to do is to change the content that Jetty Proxy get from the backend server. And forward it back to the client.
I got problem when doing this. It seem that the byte array that I get from ContentExchange couldn't transfer to String correctly.
I've try the following codes:
ContentExchange exchange = new ContentExchange() {
     protected void onResponseComplete() throws IOException {
      super.onResponseComplete();
      //do something with the response content
      String responseContent = this.getResponseContent();
      System.out.println(responseContent); // this prints messy code in console.
       byte[] responseContentBytes = this.getResponseContentBytes();   
       System.out.println(new String (responseContentBytes,"UTF-8")); // Still prints out messy code, the html's charset is UTF-8
     }
 
 
I test the following code in my developing environment, it works well:
public static void main1(String[] args) throws Exception {  
        HttpClient httpClient = new HttpClient();  
        // set up httpClient  
        httpClient.start();  
        ContentExchange contentExchange = new ContentExchange();  
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);  
        contentExchange.setURL("http://www.google.com.hk");  
        httpClient.send(contentExchange);  
        contentExchange.waitForDone();  
        System.err.println("Response status: " 
                + contentExchange.getResponseStatus());  
        byte[] responseContentBytes = contentExchange.getResponseContentBytes();  
        System.out.println(new String (responseContentBytes,"UTF-8"));  
    }
 
So, my question is: what cause the messy code problem? How can I resolve the response content from ContentExchange's getResponseContentBytes() or getResponseContent() correctly?
 
Thanks very much!
Regards!
Santa

Back to the top