2017-04-25 3 views
0

私はApp Engineに配備しました。私はSSLをセットアップし、カスタムドメインに関連付けました。アプリケーションをローカルで開発していて、http://localhost:8080/servlet経由でサーブレットに送信すると、期待通りに機能しましたが、App Engineにデプロイするときに、まだ適切な結果が得られていません。私は多くのことを試してきましたが、私が得意な応答コードは404か500です。JavaクライアントアプリケーションからGoogle App Engine上のサーブレットに適切に投稿する方法

サーブレットにJSONを送り、適切な応答を得るために単純なHTTPUrlConnectionとDataOutputstreamで始めました。ように:

URL url; 
    HttpURLConnection connection = null; 
    try { 
     url = new URL(targetURL); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", "application/json"); 
     connection.setRequestProperty("custom-Header", "XYZ"); 
     connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.length)); 
     connection.setRequestProperty("Content-Language", "en-US"); 

     connection.setUseCaches(false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 

     //Send request 
     DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
     wr.write(urlParameters); 
     wr.flush(); 
     wr.close(); 

     //Get Response  
     BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while((line = rd.readLine()) != null) { 
      response.append(line); 
      response.append('\r'); 
     } 
     rd.close(); 
     return response.toString(); 

    } 
    finally { 
     if(connection != null) { 
      connection.disconnect(); 
     } 
    } 

これはローカルで動作します。私は今それ多分タイミングの問題かどうかを確認するために、Apacheの共通のHttpAsyncClientを試してみた

 final ResponseObject responseObject = new ResponseObject(); //my simple POJO 

     try(CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() 
      .setSSLStrategy(sslSessionStrategy) 
      .build()) { 

     httpclient.start(); 

     HttpPost post = new HttpPost(url); 
     post.setHeader("Content-Type", "application/json"); 
     post.setHeader("custom-Header", "XYZ"); 
     post.setHeader("Content-Language", "en-US"); 

     HttpEntity entity = new ByteArrayEntity(urlParameters); 
     post.setEntity(entity); 

     final CountDownLatch latch1 = new CountDownLatch(1); 
     httpclient.execute(post, new FutureCallback<HttpResponse>() { 

      public void completed(final HttpResponse response) { 
       int status = response.getStatusLine().getStatusCode(); 
       if (status >= 200 && status < 300) { 
        HttpEntity entity = response.getEntity(); 
        try { 
         responseObject.message = entity != null ? EntityUtils.toString(entity) : null; 
        } catch (ParseException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } else { 
        responseObject.exception = new ClientProtocolException("Unexpected response status: " + status); 
       } 

       latch1.countDown(); 
      } 

      public void failed(final Exception ex) { 
       responseObject.exception = ex; 
       latch1.countDown(); 
      } 

      public void cancelled() { 
       latch1.countDown(); 
      } 

     }); 

     latch1.await(); 
     if(responseObject.exception != null) { 
      throw responseObject.exception; 
     } else { 
      return responseObject.message; 
     } 
    } 

これは、ローカルでも動作しますが、AppEngineの、まだ行くに到達しようとするとき。

<servlet> 
    <servlet-name>login</servlet-name> 
    <servlet-class>my.servlet.package.LoginServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>login</servlet-name> 
    <url-pattern>/login</url-pattern> 
</servlet-mapping> 

<security-constraint> 
<web-resource-collection> 
    <web-resource-name>everything</web-resource-name> 
    <url-pattern>/*</url-pattern> 
</web-resource-collection> 
<user-data-constraint> 
    <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
</user-data-constraint> 
</security-constraint> 

<welcome-file-list> 
    <welcome-file>home.jsp</welcome-file> 
</welcome-file-list> 

は局所的には、私はhttp://localhost:8080/loginに投稿:

は、ここに私の簡単なweb.xmlのです。 App Engineのためとして、私はFFに投稿:

私は、URLパターンを変更しようとしました。私は/loginで始まり、次にログインを実行してから、App EngineとカスタムドメインのURL(myapp.appspot.com/loginとmyapp.mydomain.com/login)を明示的に試しました。また、サーブレットに関連付けられた実際のページ(login.jspまたはlogin.html)を持たないと、実際のjspまたはhtmlページを投稿しようとしました。

HttpAsyncClient(SSLContextの私の選択)を使用したとき、私が得た最良の結果はサーブレットに関連付けられたページのHTMLでしたが、サーブレットから必要な応答は得られませんでした。

アイデア? https://cloud.google.com/appengine/docs/standard/java/how-requests-are-handled https://cloud.google.com/appengine/docs/standard/java/how-requests-are-routed

基本的には、例えば、プロジェクトのappspotのURLを付加する必要があります:Googleのクラウドプラットフォームの散乱ドキュメントの一つの答えが見つかり

+0

誰ですか?誰も? –

答えて

0

myapp.appspot.comに、アプリのアクティブなインスタンスのバージョンIDを入力します。このようなIDは、App Engineコンソールの[バージョン]ページで確認できます。例:https://versionId.myapp.appspot.com

関連する問題