2011-07-12 28 views
2

我的问题是getInputStream()挂起时,我试图打开一个流发布到服务器后。用python编写的服务器给出了一个错误消息,导致我相信它无法正确解析我的查询字符串。请帮我诊断这个问题。getInputStream()挂在POST请求

我张贴与下面的代码的Web服务器:

String boundarySolo = "--158211729626281"; 
String boundary = boundarySolo + "\r\n"; 
String contentHeader = "Content-Disposition: form-data; name=\""; 
URL requestUrl; 
URLConnection connection; 
String queryString = new String(); 

try{ 
    requestUrl = new URL(config.getServerUrl()); 
    connection = requestUrl.openConnection(); 
    connection.setReadTimeout(1000); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 

    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundarySolo); 

。 。 。

outStream = new DataOutputStream(connection.getOutputStream()); 

    System.out.println("Writing bytes..."); 
    outStream.writeUTF(queryString); 
    System.out.println("Bytes written"); 
    outStream.flush(); 
    System.out.println("Buffer flushed."); 
    outStream.close(); 
    System.out.println("Stream closed."); 

    try{ 
    inStream = new DataInputStream(connection.getInputStream()); 
    String buffer; 

    System.out.println("Entering the loop."); 

    while((buffer = inStream.readLine())!= null) 
     System.out.println(buffer); 

    System.out.println("Leaving the loop."); 

    } 
    catch (SocketTimeoutException e) { 
    System.out.println("Socket timed out."); 
    } 

查询字符串由这个(与\r\n在每一行的末尾):

--158211729626281 
    Content-Disposition: form-data; name="view" 

    a 
    --158211729626281 
    Content-Disposition: form-data; name="termdb" 

    Saccharomyces_cerevisiae.etd 
    --158211729626281 
    Content-Disposition: form-data; name="raw_weights" 

    blank 
    --158211729626281 
    Content-Disposition: form-data; name="MAX_FILE_SIZE" 

    256 
    --158211729626281 
    Content-Disposition: form-data; name="cutoff_Evalue" 

    0.01 
    --158211729626281 
    Content-Disposition: form-data; name="min_term_size" 

    2 
    --158211729626281 
    Content-Disposition: form-data; name="effective_tdb_size" 

    0 
    --158211729626281 
    Content-Disposition: form-data; name="stats" 

    wsum 
    --158211729626281 
    Content-Disposition: form-data; name="transform_weights" 

    null 
    --158211729626281 
    Content-Disposition: form-data; name="cutoff_type" 

    none 
    --158211729626281 
    Content-Disposition: form-data; name="output" 

    tab 
    --158211729626281-- 
+0

什么错误讯息您在服务器获取呢?然后,您可能希望将其重新构建为Python目标问题。 – BalusC

回答

0

尝试使用类似http://apikitchen.com/ 的东西来测试您的有效负载并查看返回的内容。

另一种方法是避免自己完成此操作。 HTTPClient,Jersey Client和Resty(我是作者)解决了这个问题,而不必深入到HTTP和MIME类型的深处。

下面是一个例子Resty让你开始:

import us.monoid.web.Resty; 
import static us.monoid.web.Resty.*; 

Resty r = new Resty(); 
String result = r.text(config.getServerUrl(), 
     form(data("MAX_FILE_SIZE","256"), 
      data("stats", "wsum"),...etc.etc.).toString(); 

(假设文本/ *正在由服务器返回)

+0

我最终使用HttpClient。谢谢。 –

6

如果我没记错的话,URLConnection的#的getInputStream()实际上是用于发送请求到服务器(如果写入输出流的请求不够大而不能导致刷新)。然后挂起,直到从服务器刷新响应。

你可以在服务器上进行调试吗?这样你就可以知道它到底有多远以及为什么连接保持打开状态。如果服务器要关闭连接,我希望在客户端发生异常。

如果您使用的是Eclipse,请使用Window菜单 - > Show View - > Other并选择TCP/IP Monitor。你可以使用它来真正看到通过电线发送的内容。

+3

+1提示到Eclipse内部的TCP/IP监视器 –

+0

以防万一:如果你想要TCP/IP监视器,你需要在Eclipse中安装Web Developer Tools – golimar