【问题标题】:HTTP method GET is not supported by this URL- Google App Engine此 URL 不支持 HTTP 方法 GET - Google App Engine
【发布时间】:2015-03-17 16:24:29
【问题描述】:
   public class MailHandlerServlet extends HttpServlet { 
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    try {

    Properties props = new Properties();
    Session session1 = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session1, req.getInputStream());

     //Extract out the important fields from the Mime Message
     String subject = message.getSubject();    
    String contentType = message.getContentType();

    printParts(message);
    //Parse out the Multiparts
    //Perform business logic based on the email
    }
    catch (IOException | MessagingException ex) {

     }
    }

    private static void printParts(Part p) throws IOException, MessagingException {
    Object o = p.getContent();

    if (o instanceof String) {
        out.println("This is a String");
    out.println((String)o);
    }
    else if (o instanceof Multipart) {
    out.println("This is a Multipart");
    Multipart mp = (Multipart)o;

    int count = mp.getCount();
    for (int i = 0; i < count; i++) {
    printParts(mp.getBodyPart(i));
    }
    }
    else if (o instanceof InputStream) {
    out.println("This is just an input stream");
    InputStream is = (InputStream)o;
    int c;
    while ((c = is.read()) != -1)
    out.write(c);
    }
    }

    }

上面的代码给了我一个这样的错误......“这个 URL 不支持 HTTP 方法 GET”请帮我修复它。我在 GAE 中部署它。我正在编写此代码来接收到我的appspot.com 的邮件。我也更新了我的 appengine-web.xml 和 web.xml。当我尝试运行该页面时,它向我显示此错误。

【问题讨论】:

  • 你已经实现了doPost,它是针对 POST 请求的。对于 GET 请求,您需要 doGet 方法

标签: google-app-engine servlets cloud


【解决方案1】:

此 servlet 中没有 doGet 方法,您正在对它进行 GET 调用。您必须进行 POST 调用或实现 GET 方法。例如:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    doPost(request, response);
}

【讨论】:

  • 好的!!明白了!我已将其更改为 doPost,现在错误为“错误:此 URL 不支持 HTTP 方法 POST”如何排除这种情况?
  • 您应该同时拥有 doPostdoGet 方法 - 如果需要,其中一个可以为空。如果在那之后你仍然收到这个错误消息,那意味着你调用了错误的 servlet。
【解决方案2】:

最近,我也在研究名为 DrEdit 的谷歌应用引擎示例 Web 应用程序,我也遇到了这个错误。我编写了以下代码以使其工作。

@Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
      System.out.println("start of GoogleDriveAuth:::::::");
      credentialManager = new CredentialManager(
                getClientSecrets(), TRANSPORT, JSON_FACTORY);
      handleCallbackIfRequired(req, resp);

您需要为您的应用程序编写类似的 doGet 方法。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多