2017-02-03 17 views
-1

私はウェブプロキシを作成していますが、これまでクライアントからGETリクエストを読んでフォーマットし、私はサーバーから応答を返すことができたと考えていますが、クライアントに応答を送信する方法が不明です。サーバからHTTPレスポンスを読み込んでクライアントに返信する方法

Scanner readClient = new Scanner(new InputStreamReader(client.getInputStream()));    

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
System.out.println("Client Request: "); 

     String request; 
     String host = ""; 
     String path = ""; 
     String[] parts = new String[4]; 

     while((request = bufferedReader.readLine())!= null) { 
      if (request.indexOf("deflate") != -1) { 
       break; 
      } 

      if(request.indexOf("GET") != -1){ 
       parts = request.split(" "); 
       path = parts[1]; 
       System.out.println("THIS IS THE PATH: " + path); 
      } 

      if(request.indexOf("Host") != -1){ 
       parts = request.split(": "); 
       host = parts[1]; 
       System.out.println("THIS IS THE HOST: " + host); 
      } 


      System.out.println(request); 
     } 

     Socket server = new Socket(host, 80); 
     System.out.println("Successfully connected to host: " + host); 

     PrintWriter writeServer = new PrintWriter(new DataOutputStream(server.getOutputStream()));   
     InputStream readServer = server.getInputStream(); 

     writeServer.print("GET " + path + "\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); 
     writeServer.flush(); 


    OutputStream writeClient = client.getOutputStream(); 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte buffer[] = new byte[1024]; 
    for(int s; (s=readServer.read(buffer)) != -1;) 
    { 
     baos.write(buffer, 0, s); 
    } 
    byte result[] = baos.toByteArray(); 

    System.out.println("message sent"); 

    } 
    catch (Exception e) { 
     System.out.println("Start Exception: " + e.getMessage()); 
    } 

} 

**私は疑問に行われた編集内容を記録することが出来るのですが、私は私の文言を変更し、それの多くを含めるだけでなく、自分のコードを更新しているかわかりません。

+0

どのような出力を期待していますか? Webページからのテキストが正しい応答になる可能性があります。 –

+0

wgetで周りを回って、Webサーバーが何を返しているのかを確認し、プログラムが行っていることと比較してください。 – Duston

+0

HTTP応答が必要です。ウェブページのテキストはそうではありません。私は応答を取得した後、私はクライアントにそれを送信し、Webページをロードします。私は端末にテキストが欲しくないので、私はそれを印刷しているので、何がサーバから戻ってくるのかを見ることができます。 – AndieM

答えて

-1

キャッチしようとしているエラーの種類は何ですか? Scanner(URL.openStream())を使用して最後にいくつかの宿題をしましたが、ブラウザにエラーとして表示される "正常ではない"ものについてはExceptionをスローします。ここに私のcatch()ステートメントがあり、それは私が当時必要だったもののために働いた。あなたは、コンテンツの長さまたは何のいずれかを排出する際

 // do we have an error? 
     catch (Exception ex) { 
      // rather than specific exceptions related to the type of 
      // error (network, protocol, webserver content/configuration) 
      // the java.net.URL.openStream(URL) seems to return 
      // a different message in .getMessage() that you have to 
      // parse to figure out what happened. 

      // would these messages be different in a different java/jvm implementation? 

      String errorMsg=ex.getMessage(); 

      // nicer errors below 
      //System.out.println("Error: "+errorMsg+"\n\r"); 

      // what makes up our URL? this lets us get the hostname 
      // easily as urlParts[2]. 
      String[] urlParts=theURL.split("/"); 

// on DNS failure (use http://aintthere.example.com as test URL) 
// Exception.getMessage() seems to return the desired hostname 

      if(errorMsg.indexOf(urlParts[2])==0){ 
       System.out.println("DNS error - invalid or unknown hostname"); 
      } 

// on a 404 error (use http://www.example.com/aintthere) the 
// Exception.getMessage() appears to return the URL requested. 

      if(errorMsg.indexOf(theURL)==0){ 
       System.out.println("The requested URL does not exist: "+theURL); 
      } 

// no route to host or host off line and/or denying connections 
      if(errorMsg.indexOf("Connection timed out")==0){ 
       System.out.println("That host is unreachable or is not allowing connections"); 
      } 

// turns out lots of different SSL errors - invalid certs, self signed certs, mis-matched hostnames, 
// all sorts of things. seems easier to parse for ".security." in the message since 
// they seem to come either from java.security.cert.* or sun.security.* 
      if(errorMsg.indexOf(".security.")!=-1){ 
       System.out.println("Insecure SSL connection attempt - not allowed"); 
      } 

// both 500 (Internal Server Error) and 403 (Access to Resource Forbidden) 
// produce nice standard looking error messages with the error number in them, so 
// we check for that. Why doesn't 404 do that? 
      if(errorMsg.indexOf("HTTP response code: 500")!=-1){ 
       System.out.println("The webserver is suffering from its own issues - Internal Server Error detected"); 
      } 

      if(errorMsg.indexOf("HTTP response code: 403")!=-1){ 
       System.out.println("Access to that resource is forbidden by the webserver configuration"); 
      } 
     } // end catch 
+0

私は技術的にエラーが発生していません。私は何らかの理由で、HTTP応答の代わりにWebページのテキストと書式をすべて取得しています。サーバーからの応答がクライアントに送信されると、ページが読み込まれるようにします。 – AndieM

+0

'HttpURLConnection'を取得してから応答コードを取得する方が簡単で賢明でした。より正確。例外メッセージのテキストとは独立しています。 – EJP

0

は、あなただけ読んで、途中の過去にコンテンツ長や転送エンコードのヘッダーに着目し、入力を出力にコピーして、停止する必要があります転送エンコーディングはレスポンスの終わりだと思っています。

関連する問題