我想用HttpURLConnection 登陆一个网站,发送POST请求,对登陆帐号和密码的字符有什么特殊的要求吗?
例如下面的:
 public static void main(String args[]) 
 { 
try{ URL url = new URL("要提交到的页面"); //就是在表单里要提交到的网页。
HttpURLConnection uc =(HttpURLConnection) url.openConnection(); 
uc.setDoOutput(true); OutputStream raw = uc.getOutputStream(); 
OutputStream buf = new BufferedOutputStream(raw); 
OutputStreamWriter out = new OutputStreamWriter(buf, "UTF-8"); 
String info=URLEncoder.encode("email=youremain&password=yourpw"); 
out.write(info);
out.flush();
out.close();InputStream in = uc.getInputStream(); 
in = new BufferedInputStream(in); 
Reader r = new InputStreamReader(in,"UTF-8"); 
int c; 
System.out.println("==================Beging===================="); 
while((c = r.read()) != -1) 
System.out.print((char) c); 
in.close(); 
System.out.println("===================End======================"); } 
catch(IOException e){ 
System.out.println("no"); 

} 总是返回首页登陆页。帐号和密码怎么没传过去。请教~~~~~~~~

解决方案 »

  1.   

    如果表是get方法的话你这样行,post应该就可能不行。 SERVLET 里读get 和 post过来的东西是一样的办法,不过有的CG
    I就用不同的方法取得get 和 post过来的东西。
      

  2.   

    要登陆的网站是用php做的,那应该怎么办呢?
      

  3.   

    发送的数据包不完整,利用抓包工具抓一个完整的数据包,用out.write()方法发送过去
    php做的一样
    利用HttpURLConnection模拟提交表单,对Cookie很难管理
    最好尝试一下Httpclient包,功能强大