2013-04-11 81 views
-1

我有一个代码,但它只打印状态,我想要的是我的代码在控制台上引发整个代码“响应”。从HttpURLConnection的如何从java中的http请求生成完整的响应代码?

import java.io.IOException; 
import java.net.URL; 
import java.net.HttpURLConnection; 

public class API{ 
    public static void main(String args[]) throws IOException 
    { 
     URL url = new URL("http://testpath"); 
     HttpURLConnection http = (HttpURLConnection)url.openConnection(); 
     int statusCode = http.getResponseCode(); 
     System.out.println(statusCode); 
    } 
} 

回答

0

这必须工作..

String statusCode = http.getResponseMessage(); 
System.out.println(statusCode); 
+0

如果我想查看该响应的来源是什么? – 2013-04-11 11:10:53

0

使用getResponseMessage()方法来获取完成者响应消息

1

你的类名为API但你似乎没有仔细阅读HttpURLConnection类的API。 :[

getResponseMessage()可能是你想要的。

如果你想获得一定的头,你将需要使用定义的属性在URLConnection

The following methods are used to access the header fields and the contents after the connection is made to the remote object: 

getContent 
getHeaderField 
getInputStream 
getOutputStream 

Certain header fields are accessed frequently. The methods: 

getContentEncoding 
getContentLength 
getContentType 
getDate 
getExpiration 
getLastModifed 
相关问题