实现:利用URL访问URL并传递参数,然后获取返回结果。
可用于模拟http访问。
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class JavaPostTest {
public String post() throws Exception {
URL url = new URL("http://www.renren.com/PLogin.do");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("email=pk3589@163.com&password=123");
writer.flush();
writer.close();
InputStreamReader reder = new InputStreamReader(conn.getInputStream(), "utf-8");
BufferedReader breader = new BufferedReader(reder);
BufferedWriter w = new BufferedWriter(new FileWriter("d:/1.txt"));
String content = null;
String result = null;
while ((content = breader.readLine()) != null) {
result += content + "\n";
}
w.write(result);
w.flush();
w.close();
return result;
}
public static void main(String[] args) throws Exception {
JavaPostTest post = new JavaPostTest();
String aa = post.post();
String bb = new String(aa.getBytes("utf-8"));
System.out.println(bb);
}
}