最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

JAVA用POST向网页发送请求,接收数据

程序问题 hanlei 1017浏览

最近想让我的俄罗斯方块的成绩能保存到网上
于是找到了下面的文章,还没有测试,先帖上来了
如何执行一个POST指令提交到网上而不执行浏览器***作,其实,很多应用程序已经用到了这项技术。曾看到一文讲述QQ外挂程序,就曾提到:QQ的服务端只不过是一个处于监听状态的socket,各个终端实际上就是通过POST方式提交请求的。
下面是一个范例程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author tensho
*/
public class TestPost {
private URL url;
private URLConnection conn;
public TestPost() {
}
public void setURL(String urlAddr) {
try {
url = new URL(urlAddr);
conn = url.openConnection();
//conn.setRequestProperty(“Pragma:”, “no-cache”);
//conn.setRequestProperty(“Cache-Control”, “no-cache”);
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void sendPost(String post) {
conn.setDoInput(true);
conn.setDoOutput(true);
PrintWriter output;
try {
output = new PrintWriter(conn.getOutputStream());
output.print(post);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getContent() {
String line, result = “”;
try {
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = in.readLine()) != null) {
result += line + “n”;
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String urlAddr = “http://localhost:7001/safety/identifyReport.jsp”;
String post = “manage_dept=%B7%BF%B9%DC%BE%D6&danger_level=3&year=2005&identify_date=2005”;
TestPost test = new TestPost();
test.setURL(urlAddr);
test.sendPost(post);
System.out.println(test.getContent());
}
}
执行这个程序,返回的就是String post位参数POST到目标地址urlAddr的结果页面的html代码,我在此以流的形式捕获令其输出。
这个程序说明2个问题:第一,POST的参数是可以用上述方法封装的;第二,可以得到返回的内容。
下面是另一者篇
Java技巧:Java向Web站点发送POST请求
向Web站点发送GET请求是很容易的。比如,创建一个URL对象http://www.sun.com/somepage.jsp?key=value,而不是http://www.sun.com/somepage.jsp。然而如果你需要发送一个POST请求时又该怎样做呢?
向一个Web站点发送POST请求只需要简单的几步。首先要和URL下的URLConnection对话。URLConnection可以很容易的从URL得到。比如:
// Using java.net.URL and
//java.net.URLConnection
URL url = new
URL(“http://jobsearch.dice.com/jobsearch/jobsearch.cgi”);
URLConnection connection = url.openConnection();
然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:

connection.setDoOutput(true);
最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如:

OutputStreamWriter out = new
OutputStreamWriter(uc.getOutputStream(), “8859_1”);
out.write(“username=bob&password=”+password+””);
// remember to clean up
out.flush();
out.close();
这样就可以发送一个看起来象这样的POST:
POST /jobsearch/jobsearch.cgi HTTP 1.0
ACCEPT: text/plain
Content-type: application/x-www-form-urlencoded
Content-length: 99
username=bob
password=someword
一旦发送成功,用以下方法就可以得到服务器的回应:
connection.getInputStream();
一些Web站点用POST形式而不是GET,这是因为POST能够携带更多的数据,而且不用URL,这使得它看起来不那么庞大。使用上面列出的大致的代码,Java代码可以和这些站点轻松的实现对话

转载请注明:HANLEI'BLOG » JAVA用POST向网页发送请求,接收数据