更多精彩请到 http://www.139ya.com
Windows查看端口被某个进程占用方法 : http://hi.baidu.com/haohaopc/blog/item/d757a44b2df19ff382025c9e.html
在命令提示符下运行netstat -ano 后面即可显示出相应的PID号,然后再到任务管理器看查看相应的PID号进程即可。
S3 put object upload file,被AI欺骗的一天 - 浙林龙哥
4 months ago
更多精彩尽在139ya网址导航 www.139ya.com
URL | http://blog.csdn.net/dragondwy |
URN | urn: |
import java.net.*;
import java.io.*;
public class testPost {
public static void main() {
URL url = null;
HttpURLConnection conn = null;
try {
url = new URL("http://localhost/projectname/servletname"); //构造好这个URL对象,参数就是你要通信的servlet地址,实际测试的时候,这个URL可以从Properties文件中取得,以增加灵活性
conn = (HttpURLConnection)url.openConnection(); //打开,创建Connection对象
conn.setRequestMethod("POST"); //设定请求方式为POST
conn.setDoOutput(true); //一定要设为true,因为要发送数据
//下面开始设定Http头
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=Bounday---");
conn.setRequestProperty("Cache-Control","no-cache");
.......
// 传送送据
OutputStream buf = conn.getOutoutStream();
buf = new BufferedOutputStream(buf);
OutputStreamWriter out = new OutputStreamWriter (buf);
out.write("这里是要传送的数据");
//比方说如下的格式,当然这是自己规定的格式,这些都可以从配置文件中设定,然后读取
//Bounday---
//Content-Disposition: form-data; name="testRequestHeader"
// Data = aabbccddeeffgghh
//--Bounday---
out.flush(); //这个一定要
out.clost();
//接收数据
InputStream in = conn.getInputStream();
in = new BufferedOutputStream(buf);
Reader rData = new InputStreamReader(in);
int c;
System.out.println("=====================Result==========================");
while((c=rData.read()) != -1)
System.out.print((char)c);
System.out.println("===================================================");
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
1. import java.io.*;
2. import java.util.*;
3. import java.net.*;
4.
5. public class WebTest {
6.
7. public static void main(String[] args) {
8.
9. System.out.println("beging...");
10. DownLoadPages("http://login.xiaonei.com/Login.do", "d:/fileDown.txt");
11. // visit("http://www.xiaonei.com");
12. System.out.println("end.");
13. }
14.
15. public static void DownLoadPages(String urlStr, String outPath) {
16. int chByte = 0;
17.
18. URL url = null;
19.
20. HttpURLConnection httpConn = null;
21.
22. InputStream in = null;
23.
24. FileOutputStream out = null;
25.
26. try {
27. String post = "email=" + URLEncoder.encode("e-mail", "UTF-8")
28. + "&password=" + "password";
29. url = new URL(urlStr);
30.
31. httpConn = (HttpURLConnection) url.openConnection();
32.
33. //setInstanceFollowRedirects can then be used to set if
34. //redirects should be followed or not and this should be used before the
35. //connection is established (via getInputStream, getResponseCode, and other
36. //methods that result in the connection being established).
37.
38. httpConn.setFollowRedirects(false);
39.
40. //inorder to disable the redirects
41. httpConn.setInstanceFollowRedirects(false);
42.
43. httpConn.setDoOutput(true);
44. httpConn.setDoInput(true);
45. httpConn.setRequestProperty("User-Agent",
46. "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
47. httpConn.setRequestProperty("Content-Type",
48. "application/x-www-form-urlencoded");
49.
50. //ok now, we can post it
51. PrintStream send = new PrintStream(httpConn.getOutputStream());
52. send.print(post);
53. send.close();
54. URL newURL = new URL(httpConn.getHeaderField("Location"));
55. System.out.println("the URL has move to "
56. + httpConn.getHeaderField("Location"));
57. httpConn.disconnect();
58.
59. // OK, now we are ready to get the cookies out of the URLConnection
60. String cookies = getCookies(httpConn);
61. System.out.println(cookies);
62. httpConn = (HttpURLConnection) newURL.openConnection();
63. httpConn.setRequestProperty("User-Agent",
64. "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
65. httpConn.setRequestProperty("Content-Type",
66. "application/x-www-form-urlencoded");
67. httpConn.setRequestProperty("Cookie", cookies);
68.
69. httpConn.setDoInput(true);
70. in = httpConn.getInputStream();
71. out = new FileOutputStream(new File(outPath));
72.
73. chByte = in.read();
74. while (chByte != -1) {
75. out.write(chByte);
76. //System.out.println(chByte);
77. chByte = in.read();
78. }
79. } catch (Exception e) {
80. e.printStackTrace();
81. }
82. }
83.
84. public static String getCookies(HttpURLConnection conn) {
85. StringBuffer cookies = new StringBuffer();
86. String headName;
87. for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {
88. StringTokenizer st = new StringTokenizer(headName, "; ");
89. while (st.hasMoreTokens()) {
90. cookies.append(st.nextToken() + "; ");
91. }
92. }
93. return cookies.toString();
94. }
95. }