求高手将C#代码转换为Java代码,关于获取网页信息的代码,一些C#中的类不知道如何对应Java类。
privateStringload(SwitchInfoBeanswitchInfo){try{Stringrouterip=switchInfo.RouterIp;St...
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))这一句该如何改写?
求高手帮忙,在线等!答案准确,就加分! 展开
{
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
java.net.CookieManager cm=new java.net.CookieManager();
cm.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(cm);
//关于2
java.net.URL url=null;
java.net.URLConnection conn=null;
String loadAddress="http://192.168.1.1";//your url
try{
url=new java.net.URL(loadAddress);
conn=url.openConnection();
}catch(Exception e){e.printStackTrace();}
if(conn!=null){
//some code here
}
//关于3
String userpass="admin";//your password
String base64=new sun.misc.BASE64Encoder().encode(userpass.getBytes());
//...
//关于1
java.net.CookieManager cm=new java.net.CookieManager();
cm.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(cm);
//关于2
java.net.URL url=null;
java.net.URLConnection conn=null;
String loadAddress="http://192.168.1.1";//your url
try{
url=new java.net.URL(loadAddress);
conn=url.openConnection();
}catch(Exception e){e.printStackTrace();}
if(conn!=null){
//some code here
}
//关于3
String userpass="admin";//your password
String base64=new sun.misc.BASE64Encoder().encode(userpass.getBytes());
//...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询