Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi their,

i'm looking for assistance to convert my Java code to c#.net code.
i have been trying for last two days using SHARPEN and SOURCE-FORGE plugins. but their is no progress.its breaking my heads :doh: :confused:
i din find any proper resource to accomplish my task.


if u know ,help me out
thanks in advance

package com.viadeo.servlets;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.json.JSONException;
import org.json.JSONObject;
 
public class Oauth2SampleCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 
	private static final String clientId = "[your client id]";
	private static final String clientSecret = "[your client secret]";
	 
	private static final String oauthBase = "https://secure.viadeo.com/oauth-provider";
	private static final String authorizeURL = oauthBase + "/authorize2";
	private static final String accessTokenURL = oauthBase + "/access_token2";
	private static final String apiBase = "https://api.viadeo.com";
	 
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	 
		String code = request.getParameter("code");
		String redirectUri = request.getRequestURL().toString();
		 
		if (code == null || "".equals(code)) {
		 
			// Step 1 - Redirect user to provider for authorization
			String url = authorizeURL + "?response_type=code&display=popup&lang=en&client_id="
			+ clientId + "&redirect_uri=" + redirectUri;
			response.sendRedirect(url);
		 
		} else {
		 
			// Step 2 - Exchange for access grant
			String urlParameters = "grant_type=authorization_code&client_id=" + clientId
			+ "&client_secret=" + clientSecret
			+ "&redirect_uri=" + redirectUri + "&code=" + code;
			 
			String resp = executePost(accessTokenURL, urlParameters);
			 
			String accessToken = "";
			try {
				JSONObject json = new JSONObject(resp);
				accessToken = json.getString("access_token");
			} catch (JSONException e) { }
			 
			// Step 3 - Create connection
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", apiBase + "/me.xml?access_token=" + accessToken);
		}
	}
	 
	// POST request helper
	private String executePost(String targetURL, String urlParameters) {
	URL url;
	HttpURLConnection connection = null;
	try {
	// Create connection
	url = new URL(targetURL);
	connection = (HttpURLConnection) url.openConnection();
	connection.setRequestMethod("POST");
	connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	connection.setRequestProperty("Content-Length",
	"" + Integer.toString(urlParameters.getBytes().length));
	connection.setRequestProperty("Content-Language", "en-US");
	 
	connection.setUseCaches(false);
	connection.setDoInput(true);
	connection.setDoOutput(true);
	 
	// Send request
	DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
	wr.writeBytes(urlParameters);
	wr.flush();
	wr.close();
	 
	// Get Response
	InputStream is = connection.getInputStream();
	BufferedReader rd = new BufferedReader(new InputStreamReader(is));
	String line;
	StringBuffer response = new StringBuffer();
	while ((line = rd.readLine()) != null) {
	response.append(line);
	response.append('\r');
	}
	rd.close();
	return response.toString();
	 
	} catch (Exception e) {
	e.printStackTrace();
	return null;
	} finally {
	if (connection != null) {
	connection.disconnect();
	}
	}
	}
}
Posted
Updated 5-Jul-11 4:41am
v2
Comments
Nagy Vilmos 5-Jul-11 10:42am    
Moved code to question from answer and deleted answer

Instead of stating the problem statement, better would be to share the issue/error and see if it helps.

Right now, I am not sure how are you expecting any of us to help you. What is the question that we should look at here? Are we suppose to suggest some software to you that does this Java to C#?
Even, if we have some software that does it, there will mostly be few errors that you need to look at yourself manually. No software can convert it 100%.

So, I would suggest you to share the issues/error and related code to get help.
 
Share this answer
 
Comments
Balaji_Reddy 22-Feb-11 4:00am    
thanks for Ur reply first of all :) yes i agree tat no software will done it 100%. is their any free software tool to convert Java code to c# code? if so, let me know .
Have you considered Google?
A very quick search (less than a minute) got me to Microsoft Java language conversion assistant[^]
 
Share this answer
 
Comments
Balaji_Reddy 22-Feb-11 4:06am    
yeah i tried in google also.and u have given link for JLCA. i wan my project manager wants to use some other tool or plugins to accomplish this task bcz of version issues i mean JLCA has been removed from Visual studio from 2008 versions. so tis will supports only for .net 2.0 tats the problem why we are looking some alternatives. i hope u would have understood:)is their any step by step kinda examples to learn easily? thanks in advance
C#
import java.io.*;
import java.net.*;

public class Lookup 
{
	public static void main(String[] args) throws Exception 
	{
		System.out.println("Hello World!");
		String client = "api";
		String apikey = "ABQIAAAAdGnGSqPru2GZ5ZCwFZsLDhQM9mDvw_EbadXXtLQ5SlzX_s0ijA";
		String appver = "1.5.2";
		String pver = "3.0";
		String url = "http://www.yahoo.com";
		String uri = "https://sb-ssl.google.com/safebrowsing/api/lookup?client="+client+"&apikey="+apikey+"&appver="+appver+"&pver="+pver;//+"&url="+url;

		URL lookUrl = new URL(uri);
		HttpURLConnection htCon = (HttpURLConnection) lookUrl.openConnection();
		htCon.setDoInput(true);
		htCon.setDoOutput(true);
		
		//OutputStream os = htCon.getOutputStream();
		PrintStream pos = new PrintStream(htCon.getOutputStream());
		pos.println(2);
		pos.println("http://www.asdrediff.com");
		pos.println("http://www.yahooabds.com");
		pos.close();
		
		System.out.println("respose code:" + htCon.getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(htCon.getInputStream()));
		String str = "";
		while((str = br.readLine()) != null) {
			System.out.println(str);
		}
		br.close();
		htCon.disconnect();	
		System.out.println("end");
	}
}
 
Share this answer
 
v2
Comments
rereyyht 30-Nov-11 2:32am    
And the server:

using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Collections;

class TalkServ {

System.Net.Sockets.TcpListener server;
public static Hashtable handles;
public static Hashtable handleByConnect;

public static void Main() {
TalkServ TS = new TalkServ();
}

public TalkServ() {
handles = new Hashtable(100);
handleByConnect = new Hashtable(100);
server = new System.Net.Sockets.TcpListener(4296);
while(true) {
server.Start();
if(server.Pending()) {
N.Sockets.TcpClient connection = server.AcceptTcpClient();
Console.WriteLine("Connection made");
BackForth BF = new BackForth(connection);
}
}
}

public static void SendToAll(string name,string msg) {
StreamWriter SW;
ArrayList ToRemove = new ArrayList(0);
N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[TalkServ.handles.Count];
TalkServ.handles.Values.CopyTo(tc,0);
for(int i=0;i
private static byte[] readBytes(String in) throws Exception {
FileInputStream fis = new FileInputStream(in);
int numOfBytes = fis.available();
byte[] buffer = new byte[numOfBytes];
fis.read(buffer);
fis.close();
return buffer;
}
 
Share this answer
 
v3

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