Click here to Skip to main content
15,921,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Java
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;
    }
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 4:59am    
Convert... a terminological curse of CodeProject.
--SA

I think this is the most similar:
C#
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;
           }
 
Share this answer
 
Comments
BobJanova 19-Jan-12 12:19pm    
Yes, that looks functionally equivalent. I'd declare the return type as bool (not Boolean), but in C# (unlike Java) they are the same thing. You'd call it like IsClickToCallCompatible(Request) from an ASP.net page method or MVC controller method (Request is available there, I think, might be Context.Request).

However, I don't see any way that this should throw except a FormatException, and you can avoid that with TryParse, viz:

bool IsClickToCallCompatible(HttpRequest request){
string userAgent = request.Headers["User-Agent"]; // I think this is capitalised
if(null == userAgent) return false;

int indexFound = userAgent.IndexOf(OS_MICROSOFT);
if(0 <= indexFound){
int version;
return 6 <= int.TryParse(userAgent.Substring(indexFound + OS_MICROSOFT.Length, 1));
} else return false;
}
You can cick the logger-entries all out:

Java
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;
    }


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 ;) )
 
Share this answer
 
v2
Comments
Richard MacCutchan 19-Jan-12 6:04am    
Also Java is better.
Yeah, that C# coffee tastes like mud. :)
TorstenH. 19-Jan-12 6:49am    
seems like I actually didn't have enough coffee - did I write "cick" up there? Must be autocowrecked.
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?
C#
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;
         }
 
Share this answer
 
C#
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);        
        DocumentBuilder domBuilder = factory.newDocumentBuilder();
        org.w3c.dom.Document document = domBuilder.parse(new InputSource(new StringReader(xml)));
        toComplexValue(document.getDocumentElement());
 
Share this answer
 
v2
package com.turkcelllab.istemci;

import com.sun.xml.internal.ws.developer.WSBindingProvider;
import com.turkcellteknoloji.lbs.LbsOpaque;
import com.turkcellteknoloji.lbs.LbsOpaqueService;
import com.turkcellteknoloji.lbs.LbsRequestOpaque;
import com.turkcellteknoloji.lbs.LbsResponse;
import com.turkcellteknoloji.lbs.ObjectFactory;
import com.turkcellteknoloji.lbs.Token;
import java.util.Arrays;

public class TurkcellLabIstemci {

private spgw.Authentication authenticationPort;
private LbsOpaque lbsOpaquePort;
private ObjectFactory lbsObjectFactory = new ObjectFactory();

public String baglan(String spUserName, String serviceVariantId, String password) {
if (authenticationPort == null) {
System.out.println("authenticationPort yaratılıyor..");
spgw.AuthenticationService authenticationService = new spgw.AuthenticationService();
authenticationPort = authenticationService.getAuthenticationPort();
System.out.println("authenticationPort yaratıldı: " + authenticationPort);
}
return authenticationPort.createSession(spUserName, serviceVariantId, password);
}

public void konum(String sessionId) throws Exception {
if (lbsOpaquePort == null) {
System.out.println("lbsOpaquePort yaratılıyor..");
LbsOpaqueService lbsOpaqueService = new LbsOpaqueService();
lbsOpaquePort = lbsOpaqueService.getLbsOpaqueSoapPort();
System.out.println("lbsOpaquePort yaratıldı: " + lbsOpaquePort);
}
WSBindingProvider bp = (WSBindingProvider) lbsOpaquePort;
Token token = new Token();
token.setSessionId(sessionId);
bp.setOutboundHeaders(lbsObjectFactory.createToken(token));
LbsRequestOpaque lbsIstek = new LbsRequestOpaque();
// lbsIstek.setCompany(null);
lbsIstek.setMsisdn("905XXXXXXXXX");
// lbsIstek.setPartnerId(null);
// lbsIstek.setRgeo(true);
lbsIstek.setVariant("11");
System.out.println("lbsIstek: " + lbsIstek);
LbsResponse lbsCevap = lbsOpaquePort.getLocation(lbsIstek);
System.out.println("lbsCevap: " + lbsCevap);
if (lbsCevap != null) {
System.out.println("lbsCevap.getStatusCode(): " + lbsCevap.getStatusCode());
if (lbsCevap.getResults() != null) {
System.out.println("lbsCevap.getResults(): " + Arrays.toString(lbsCevap.getResults().toArray()));
}
if (lbsCevap.getErrors() != null) {
System.out.println("lbsCevap.getErrors(): " + Arrays.toString(lbsCevap.getErrors().toArray()));
}
}
}

public static void main(String[] args) {
try {
TurkcellLabIstemci istemci = new TurkcellLabIstemci();
System.out.println("bağlanıyor..");
String spUserName = "905XXXXXXXXX";
String serviceVariantId = "11";
String password = "parola";
String sessionId = istemci.baglan(spUserName, serviceVariantId, password);
if (sessionId != null) {
System.out.println("baglantı başarılı sessionId: " + sessionId);
istemci.konum(sessionId);
} else {
System.out.println("bağlantı başarısız!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
Share this answer
 
Comments
[no name] 22-Sep-12 8:04am    
And this is what? What does this unformatted java code have to do with the question that was asked?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900