‘壹’ java用jpcap怎么识别出http和https的数据包
暂时的解决方法:
在抓到的tcp数据包的data字段里判断是否包含"HTTP"字符串。
不足:
1.与wireshark对比后发现,wireshark有的tcp包中data字段有HTTP也识别为tcp而非http(不知道wireshark是怎么解析的)。
2.感觉会漏判,不够准确。
继续寻找更好的解法,期待解答!
‘贰’ java怎样获取http请求的body
读取Body使用request.getReader(),但getReader获取的是BufferedReader,需要把它转换成字符串,下面是转换的方法
public class TestController {
@RequestMapping("/a")
protected void doPost(HttpServletRequest request,
HttpServletResponse response, BufferedReader br)
throws ServletException, IOException {
//Header部分
System.out.print(request.getHeaderNames());
Enumeration<?> enum1 = request.getHeaderNames();
while (enum1.hasMoreElements()) {
String key = (String) enum1.nextElement();
String value = request.getHeader(key);
System.out.println(key + "\t" + value);
}
//body部分
String inputLine;
String str = "";
try {
while ((inputLine = br.readLine()) != null) {
str += inputLine;
}
br.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println("str:" + str);
}
‘叁’ Java获取Http响应Header信息
本文中演示如何通过URLConnection获取Http响应Header信息
从响应中获得Header信息
URL obj = new URL
URLConnection conn = obj openConnection()
Map<String List<String》 map = conn getHeaderFields()
从响应Header中获取Server信息
Map<String List<String》 map = conn getHeaderFields()
List<String> server = map get( Server )
完整的示例
package qiyadeng ;
import URL;
import URLConnection;
import java util List;
import java util Map;
public class GetHttpResponseHeader {
public static void main(String[] args) {
try {
URL obj = new URL
URLConnection conn = obj openConnection()
Map<String List<String》 map = conn getHeaderFields()
誉伏带System out println( 显示响应Header信息 )
for (Map Entry<String List<String》 entry : map entrySet()) {
System out println( Key : + entry getKey() +
Value : + entry getValue())
}
System out println( 使用庆芦key获得响应Header信息 )
List<String> server = map get( Server )
if (server == null) {
System out println( Key Server is not found! )
} else {
for (String values : server) {
System out println(values)
}
}
厅兄} catch (Exception e) {
e printStackTrace()
}
}
}
输出
显示响应Header信息…
Key : null Value : [HTTP/ OK]
Key : X Pingback Value :
Key : Date Value : [Sun Mar : : GMT]
Key : Transfer Encoding Value : [chunked]
Key : Connection Value : [close]
Key : Content Type Value : [text/; charset=UTF ]
Key : Server Value : [Apache/ (CentOS)]
Key : X Powered By Value : [PHP/ ]
lishixin/Article/program/Java/hx/201311/25662
‘肆’ 如何用java实现HTTP长连接
咱的理解:
当应用程序读取完响应体内容后或者调用 close() 关闭了URLConnection.getInputStream()返回的流,
JDK中的HTTP协议句柄将关闭连接,并将连接放到连接缓存中。下次获取URLConnection示例,并获取InputStream流时,会再次使用之前打开的TCP连接。
也就是说:
public static String sendPost(String url, String param) { String result = ""; try { URL httpurl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection(); httpConn.setDoOutput(true); httpConn.setDoInput(true); PrintWriter out = new PrintWriter(httpConn.getOutputStream()); out.print(param); out.flush(); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } in.close(); } catch (Exception e) { System.out.println("没有结果!" + e); } return result; }
咱在间隔较短的时间内调用上面的方法(连接的地址不一样,但还是同一个服务器同一个项目),用的是同一个TCP连接。咱这么理解对吗?不对的话请具体讲讲。谢谢。
‘伍’ java 获得http下载文件的真实名称
有两种获取链接文件名的方法:
第一种:从连接URL,从描述中获取
比如这种地址:
http://book.booktxt.com/txtbuk/20130421/xuanhuan/2013043601180.rar
‘陆’ 求java 获取HTTP错误信息
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
HttpURLConnection con=(HttpURLConnection) new URL("启升http://www.google.com/123").openConnection();
con.setRequestMethod("HEAD"悄态老闭仿);
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
‘柒’ java怎样读取http文件服务器上的文件列表并下载
要求文件名不能写死,那么只能到服务器上去遍历目录,如果服务器开了ftp权限的话到可以用apache的commons-net包,里面有ftp功能可以上传下载文件,也可以遍历文件