2013-10-23 62 views
0

我目前正在开发一个小型邮件客户端,并且SendGrid服务用于发送/接收电子邮件。 SendGrid当前发布到我的应用程序中的一个url收到的电子邮件,到url http://myapp.com/getmail。这是在我的Play Framework应用中设置的路由:POST/getmail Application.getmail()。在getmail()中,我想连接到sendgrid发布邮件并以字节读取它的url(现在为String,以确保我能够从URL中读取)。我已经尝试下面的代码,但不工作:如何将SendGrid发布的POST表单数据发布到Java中的url

public static Result getmail() { 
try { 
     URL oracle = new URL("http://myapp.com/getmail"); 
     BufferedReader in = new BufferedReader(
     new InputStreamReader(oracle.openStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

这种失败:

java.io.FileNotFoundException: http://myapp.com/getmail 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.URL.openStream(Unknown Source) 
at controllers.Application.getmail(Application.java:143) 

什么是做到这一点的正确方法?谢谢!

回答

0

我想出了这个代码,尽量争取POST数据:

public static Result getmail() { 
    Http.Request req = Controller.request(); 
    Http.RequestBody reqbody = req.body(); 
    System.out.println("AS TEXT: " + reqbody.asText()); 
    byte[] rawbuff = reqbody.as(byte[].class); 
    String rawstr = new String(rawbuff); 
    System.out.println("REQUEST: " + rawstr); 
} 

但第一个打印输出为空,第二个失败,一个NullPointerException。任何人有任何想法为什么发生这种情况请求正文应该填写我从SendGrid获得的电子邮件。我需要它原始的字节形式,所以DynamicForm类在这里没有帮助。谢谢!

+0

好的,根据API(http://www.playframework.com/documentation/2.0.4/api/java/play/mvc/Http.Request.html#body()),body()方法是一个抽象的。我怎样才能解决这个问题,并真正得到请求的正文? – franciscb