簡體   English   中英

getInputStream() 掛起一個 POST 請求

[英]getInputStream() Hang on a POST Request

我的問題是當我在發布到服務器后嘗試打開 stream 時getInputStream()掛起。 用 python 編寫的服務器給出了一條錯誤消息,讓我相信它無法正確解析我的查詢字符串。 請幫我診斷這個問題。

我使用以下代碼發布到 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--

如果我沒記錯的話, URLConnection#getInputStream() 是實際將請求發送到服務器的內容(如果寫入 output stream 的請求不足以導致刷新)。 然后它會掛起,直到從服務器刷新響應。

可以在服務器上調試嗎? 這樣你就可以知道它有多遠以及為什么連接保持打開狀態。 如果服務器要關閉連接,我預計客戶端會出現異常。

如果您使用 Eclipse,請使用 Window 菜單 -> 顯示視圖 -> 其他和 select TCP/IP 監視器。 您可以使用它來真正查看通過網絡發送的內容。

嘗試使用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();

(假設服務器正在返回 text/*)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM