簡體   English   中英

如何獲取HttpResponse文本

[英]How to get a HttpResponse text

我正在使用rest上載文件,並從服務器獲得響應,以防萬一上載成功(響應代碼200),並且我也對此操作獲得了指導,標題看起來像這樣:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 36
Date: Wed, 26 Jun 2013 07:00:56 GMT

**772fb809-61d5-4e12-b6f2-133f55ed9ac7** // the guid

我不知道如何才能拔出這個向導? 我應該使用getInputStream()嗎?

10倍

您可以使用一個BufferedReader ,這是一個示例:

InputStream inputStream = conn.getInputStream(); 
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 
while(br.ready()){ 
    String line = br.readLine(); 
    //line has the contents returned by the inputStream 
}

從您共享的響應來看,似乎您是在主體中而不是在標頭中獲得guid。 標頭通常是名稱-值對。

您需要閱讀響應正文並獲得指導。 如果guid是響應中唯一的內容,則可以執行以下操作:

URL url = new URL("http://yourwebserviceurl");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String guid = IOUtils.toString(in, encoding);
System.out.println(guid );

IOUtils來自Apache。

暫無
暫無

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

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