使用urlconnection,如果需要session可将session id放入到cookie中

解决方案 »

  1.   

    URL url = new URL("http://abc.com/vote.jsp?id=1");
          HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setDoInput(true);
          connection.connect();      BufferedReader in = new BufferedReader(new
                                                 InputStreamReader(connection.
              getInputStream()));      // print first ten lines of contents      String line;      StringBuffer sb=new StringBuffer();      while ( (line = in.readLine()) != null ) {
            System.out.println(line);      }
      

  2.   

    Enumeration enumParam = req.getParameterNames();//所有参数名
        while (enumParam.hasMoreElements()) {
          String paramName = (String) enumParam.nextElement();
          //这儿在数据库中建立包含相应字段的表.
          String paramValues[] = req.getParameterValues(paramName);//参数对应值
          if (paramValues != null) {
            for (int i = 0; i < paramValues.length; i++) {
                 //这儿插入行            
            } 
          } 
      

  3.   

    如果需要Session验证,怎么办?举例:网站需要“用户名”、密码登录,并且要选择登录类型。
    例如:
       用户登录
    用户名:____
    密  码:____
      企业用户  个人用户说明:”企业用户“和”个人用户“是登录的两种类型。必须登录后采用获得 数据页面,如果直接将通过urlconnection连接数据网址,将提示“您长时间未操作,请重新登录”,并引导到登录页面。希望能再次提供帮助。谢谢!
      

  4.   

    这是一登陆的方式:  
     URL url = new URL("your url");
       URLConnection connection=url.openConnection();
       connection.setDoOutput(true);
       connection.setDoInput(true);
     PrintWriter out
                 = new PrintWriter(connection.getOutputStream());
             out.print("type=person&");//这些是登陆的参数
             out.print("LOGINNAME=user&");
             out.print("PASSWORD=user");         out.close();
             connection.connect();
             int n = 1;
             String key;
             while ( (key = connection.getHeaderFieldKey(n)) != null) {                          String value = connection.getHeaderField(n);
                              break;//如果connect的头中有session id,那么一般就是connection.getHeaderFieldKey(1);下次再连接的时候,用connection.setRequestProperty("Cookie",value);可将session传递过去
                                                      
                  }