import http.Http; import java.util.*; import java.io.*; public class NewClass { public static void main(String[] args) throws Exception { String charset = "gbk"; Http http1 = new Http("http://xxxxxxx/login.php", charset); /** * 登录 */ http1.addPostData("username", "username中文"); http1.addPostData("password", "password中文"); http1.execute(); /** * 另一个访问,cookie和session你都不用管了,会自动处理,会处理包括cookie的过期时间等等。这就像浏览器的两个标签页一样。 */ Http http2 = new Http("http://xxxxxxx/xxxxxx.php", charset); http2.addPostData("testfield", "testfieldvalue中文1"); http2.addPostData("mycheckbox[]", "testfieldvalue中文2"); http2.addPostData("mycheckbox[]", "testfieldvalue中文3"); http2.addUploadFile("fieldName", "F:\\NetBeansProjects\\Wrapper\\src\\upfile.txt" , "rename.txt"); http2.execute(); /** * 打印返回的结果出来 */ //状况 System.out.println("-------response header-------"); Map<String, List<String>> headersMap = http2.getHeaders(); for (Map.Entry<String, List<String>> entry : headersMap.entrySet()) { String string = entry.getKey(); List<String> list = entry.getValue(); System.out.println(string + ": " + list.get(0)); } //内容 //压缩的 if (http2.getHeader("Content-Encoding") != null) { System.out.println("-------response content 压缩的,格式为" + http2.getHeader("Content-Encoding")); //使用http2.getInputStream()得到二进制流,做其它处理,解压缩、保存到文件等等。 return; } //非压缩的 System.out.println("-------response content-------"); InputStreamReader isr = new InputStreamReader(http2.getInputStream(), charset); StringBuilder stringBuilder = new StringBuilder(); int len; char[] cbuf = new char[1024]; while ((len = isr.read(cbuf)) >= 0) { if (len == 1024) { stringBuilder.append(cbuf); } else { stringBuilder.append(cbuf, 0, len); } } System.out.println(stringBuilder.toString()); } } |