于 2003-05-03 02:51
请教高手
我如何用java模拟post方式进行登陆论坛?
我先
URLConnection uc=htpurl.openConnection();
uc.setDoOutput(true);
OutputStreamWriter out=new OutputStreamWriter(uc.getOutputStream(),"ASCII");
打开一个连接
然后构造post信息
String ep;
ep="POST "+url+" HTTP 1.1\r\n";
ep+="Content-type: app1ication/x-www-form-urlencoded\r\n";
ep+="Content-length:"+query.toString().length()+"\r\n\r\n";
ep+=query.toString();
System.out.println(ep);
再向服务器发送
out.write(ep);
out.write("\r\n");
out.flush();
out.close();
再获取回复
InputStream in= InputStream in=uc.getInputStream();
InputStreamReader r=new InputStreamReader(in);
LineNumberReader din=new LineNumberReader(r);
String line=null;
StringBuffer sb=new StringBuffer();
while((line=din.readLine())!=null)
{
sb.append(line+"\n");
}
System.out.print(sb.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
但是返回的总是没登陆的页面,为什么,高手帮忙!
why edited on 2003-06-28 19:24