将Java类转换为C# [英] Convert a java class to C#
问题描述
你好;
我想将此java方法转换为C#方法.我想知道HttpServletRequest,request.getHeader("user-agent"),logger.isdebugenabled的翻译
此类用于点击通话模块.
Hello;
I want to translate this java method into a C# method. I want to know the translation of HttpServletRequest, request.getHeader("user-agent"), logger.isdebugenabled
This class is used for click-to-call module.
private static String OS_MICROSOFT = "Windows NT ";
private Boolean isClickToCallCompatible(HttpServletRequest request) {
String userAgent = null;
int indexFound = -1;
try {
userAgent = request.getHeader("user-agent");
if (logger.isDebugEnabled())
logger.debug("Current user-agent : " + userAgent==null?"No userAgent in header":userAgent);
if (userAgent!=null)
indexFound = userAgent.indexOf(OS_MICROSOFT);
if (indexFound!=-1
&& Integer.parseInt(userAgent.substring(indexFound + OS_MICROSOFT.length(), indexFound + OS_MICROSOFT.length() +1))>=6
) {
return Boolean.TRUE;
}
} catch (Exception e) {
if (logger.isDebugEnabled())
logger.debug("recent microsoft useragent OS parsing error, assuming no Click To Call compatibility");
return Boolean.FALSE;
}
return Boolean.FALSE;
}
推荐答案
我认为这是最相似的:
I think this is the most similar:
private Boolean IsClickToCallCompatible(HttpRequest request)
{
int indexFound = -1;
try
{
var userAgent = request.Headers["user-agent"];
if (userAgent != null)
indexFound = userAgent.IndexOf(OS_MICROSOFT, StringComparison.Ordinal);
if (userAgent != null && (indexFound != -1
&&
int.Parse(userAgent.Substring(indexFound + OS_MICROSOFT.Length,
indexFound + OS_MICROSOFT.Length + 1)) >= 6)
)
return true;
}
catch (Exception)
{
return false;
}
return false;
}
您可以全部删除记录器条目:
You can cick the logger-entries all out:
private static String OS_MICROSOFT = "Windows NT ";
private Boolean isClickToCallCompatible(HttpServletRequest request) {
String userAgent = null;
int indexFound = -1;
try {
userAgent = request.getHeader("user-agent");
if (userAgent!=null)
indexFound = userAgent.indexOf(OS_MICROSOFT);
if (indexFound!=-1
&& Integer.parseInt(userAgent.substring(indexFound + OS_MICROSOFT.length(), indexFound + OS_MICROSOFT.length() +1))>=6
) {
return Boolean.TRUE;
}
} catch (Exception e) {
return Boolean.FALSE;
}
return Boolean.FALSE;
}
这些只是引用了log4j,这是附加的,常用的,并且从某种角度来说是必要的-但是代码也可以不使用.
您是否尝试过将其加载到C#Bench中?它很可能会做出反应并提出正确的建议. Java和C#差别不大. Java也更好(不需要下注;))
Those are just referencing to log4j, which is additional, common used and to some point of view necessary - but the code also runs without.
Have you tried loading it into your C# Bench? Could well be possible that it reacts and makes the right suggestions. Java and C# are not much different. Also Java is better (no need to downvote ;) )
感谢log4j的提示.当我使用此代码切换到C#时,没有编译错误,但是有疑问:HttpContext是否等效于HttpServletRequest?是GetSection = getheader吗?
Thanks for the hint on log4j. When I switch to C# with this code and there is no compile errors, but have doubts: is HttpContext equivalent to HttpServletRequest? Is GetSection = getheader?
private Boolean isClickToCallCompatible(HttpContext request)
{
int indexFound = -1;
try
{
//userAgent = request.GetHeader("user-agent");
var userAgent = (string) request.GetSection("user-agent");
if (userAgent != null)
indexFound = userAgent.IndexOf(OS_MICROSOFT, StringComparison.Ordinal);
if (userAgent != null && (indexFound != -1
&&
int.Parse(userAgent.Substring(indexFound + OS_MICROSOFT.Length,
indexFound + OS_MICROSOFT.Length + 1)) >= 6)
)
return true;
}
catch (Exception)
{
return false;
}
return false;
}
这篇关于将Java类转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!