private String load(SwitchInfoBean switchInfo)
    {
        try
        {
            String routerip = switchInfo.RouterIp;
            String switchUsername = switchInfo.SwitchUserName;
            String switchPassword = switchInfo.SwitchPassword;
            String userpass = switchUsername + ":" + switchPassword;
            String loadAddress = "http://" + routerip + ":" + switchInfo.Gblport + "/macinfo.htm";
            String sUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            String sContentType = "application/x-www-form-urlencoded";
            String sResponseEncoding = "EUC-JP";
            
            CookieContainer myCookieContainer = new CookieContainer();
            // load ------------------------START-----------------------------------
            //web request create and set
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(loadAddress);
            httpRequest.UserAgent = sUserAgent;
            httpRequest.ContentType = sContentType;
            httpRequest.getHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(userpass)));
            httpRequest.CookieContainer = myCookieContainer;
            Stream myResponseStream = httpRequest.GetResponse().GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(sResponseEncoding));
            String outdata = myStreamReader.ReadToEnd();
            //stream close
            myStreamReader.Close();
            return outdata;
            // load -------------------------END------------------------------------
        }
        catch (Exception ex)
        {
            throw new Exception("error");
        }
    }以上是C#代码,主要问题在:
CookieContainer myCookieContainer = new CookieContainer(); 
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(loadAddress);
这几个类在JAVA中如何实现?
另外之后的代码,那些属性语句如何改写?
Convert.ToBase64String(Encoding.Default.GetBytes(userpass))这一句该如何改写?求高手帮忙,在线等!

解决方案 »

  1.   

    有必要从C#往java翻译么?用java的URLConnection很简单就可以实现
      

  2.   

    如果c#和java的类与方法是一一对应的,那java和C#还有区别么?!
      

  3.   

    我也没有要求翻译啊,只是现在需要用JAVA实现上面C#实现的功能而已,现在也没人无聊到去比较C#和JAVA的类吧……另外楼上有说到URLConnection可以很简单的实现,我能力不够,还请明说……谢谢!具体怎么在HTTP的header中加入认证的用户信息,并成功打开,以读取页面。