Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从一个HTTP请求读取HTTP响应头和正文_Java_Http_Network Programming - Fatal编程技术网

在java中从一个HTTP请求读取HTTP响应头和正文

在java中从一个HTTP请求读取HTTP响应头和正文,java,http,network-programming,Java,Http,Network Programming,在我的程序中,我需要发送一个HTTP请求并读取HTTP响应正文和标题 所以我把这些例子加在一起如下: URL obj = new URL("http://localhost:8080/SpringSecurity/admin"); URLConnection conn = obj.openConnection(); //get all headers Map<String, List<String>> map = conn.getHeaderFields(); for

在我的程序中,我需要发送一个HTTP请求并读取HTTP响应正文和标题

所以我把这些例子加在一起如下:

URL obj = new URL("http://localhost:8080/SpringSecurity/admin");
URLConnection conn = obj.openConnection();

//get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
}        
ByteArrayOutputStream output = (ByteArrayOutputStream) conn.getOutputStream();        

byte[] input = output.toByteArray();         
System.out.println(input.length);
URL obj=新URL(“http://localhost:8080/SpringSecurity/admin");
URLConnection conn=obj.openConnection();
//获取所有标题
Map Map=conn.getHeaderFields();
对于(Map.Entry:Map.entrySet()){
System.out.println(“Key:+entry.getKey()+”,Value:+entry.getValue());
}        
ByteArrayOutputStream输出=(ByteArrayOutputStream)连接。getOutputStream();
字节[]输入=输出。toByteArray();
System.out.println(输入.长度);
它打印头,但不打印字节数组的长度
input


有人能解释为什么会发生这种情况,以及读取HTTP响应头和正文的示例吗

这一次我做错了,打开了一个输出流,它使用
conn.getOutputStream()
写入连接。因此,我使用
conn.getInputStream()
打开了一个从连接读取的输入流

因此,代码的正确形式是

URL obj = new URL("http://localhost:8080/SpringSecurity/admin");          
URLConnection conn = obj.openConnection();

//get all response headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
}

//get response body
InputStream output = conn.getInputStream();
Scanner s = new Scanner(output).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
System.out.println(result);
URL obj=新URL(“http://localhost:8080/SpringSecurity/admin");          
URLConnection conn=obj.openConnection();
//获取所有响应头
Map Map=conn.getHeaderFields();
对于(Map.Entry:Map.entrySet()){
System.out.println(“Key:+entry.getKey()+”,Value:+entry.getValue());
}
//获取响应体
InputStream输出=conn.getInputStream();
扫描仪s=新扫描仪(输出)。使用分隔符(\\A”);
字符串结果=s.hasNext()?s、 next():“”;
系统输出打印项次(结果);

代码不正确。您不应该同时使用
getInputStream()
getHeaderFields()
,因为您将获得不完整的数据。原因是
getHeaderFields
内部调用
getInputStream
()。有关更多详细信息,请参见此处的评论: