Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert java to c#

Status
Not open for further replies.

buxtonicer1

Programmer
Sep 20, 2006
82
GB
Is there a way to convert a java class to c# .
I'm thinking some application, website or tool to help.

Anyone here done it ?
 
Ya thats what I've been doing.

The code below is what I'm trying to convert or find c# that has similar functionality.

import java.io.*;
import java.net.*;
import java.util.Date;

/**
* This simple program uses the URL class and its openStream() method to
* download the contents of a URL and copy them to a file or to the console.
**/
public class

url_timer {

public static void main(String[] args) {
long prenow = 0;
long postnow = 0;
String urlname = "<URL>";
InputStream in = null;

try {
FileInputStream fin = new FileInputStream("d:\\temp\\urls.txt"); //Can be done in c#

// now turn the FileInputStream into a DataInputStream
try {
DataInputStream myInput = new DataInputStream(fin);

try {
while ((urlname = myInput.readLine()) != null) {

Its this functionality below that I'm trying to replicate but having difficulty in doing. The code takes a url and works out the size of the file it links to by counting the number of bytes it has. As you can see there is a time stamp before and after the process --- this is used to give an estimation of how long a file would take to download.
Does anyone know of something similar that I can use in c#
a similar url class perhaps ...... Cheers for your help


URL url = new URL(urlname);
URLConnection uc = url.openConnection();
uc.setDefaultUseCaches(false);
uc.setUseCaches(false);
uc.setRequestProperty("Cache-Control",
"max-age=0,no-cache");
uc.setRequestProperty("Pragma", "no-cache");
uc.setRequestProperty("Cache-Control", "no-cache");
in = uc.getInputStream();
byte[] buffer = new byte[4096];
int bytes_read;

prenow = System.currentTimeMillis();
System.out.print(prenow + "\n");


while ((bytes_read = in.read(buffer)) != -1)

postnow = System.currentTimeMillis();
System.out.print(postnow + "\n");
System.out.print(postnow - prenow + "\n");
// System.out.println(thisLine);
} // while loop ends here
} catch (Exception e) {
System.out.println("Error: " + e);
}
} // end try
catch (Exception e) {
System.out.println("Error: " + e);
}

} // end try

catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
 
This is what i use... I converted it to C# but it need some castings. In vb.NET that i originally have it, it run without any problem.

Code:
using System.Net;
using System.IO;

          HttpWebRequest wbrqLogin; 
          HttpWebResponse wbrsLogin;
          String strTemp;
          StreamReader srLogin;

          wbrqLogin = WebRequest.Create("[URL unfurl="true"]http://www.tek-tips.com");[/URL]
          wbrqLogin.Method = "GET";

          wbrsLogin = wbrqLogin.GetResponse();
          srLogin = new StreamReader(wbrsLogin.GetResponseStream());
          strTemp = srLogin.ReadToEnd.Trim;
          srLogin.Close();

          this.textBox1.Text = strTemp;
 
Getting the following error with this code ...

private void btnDownload_Click(object sender, System.EventArgs e)
{
//****************************************************
Uri uri = new Uri(" System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(uri);
System.IO.Stream stream = wr.GetResponse().GetResponseStream();
int b;
int counter=0;
string strResult = string.Empty;
while ((b=stream.ReadByte())!=-1)
{
strResult += System.Text.UTF7Encoding.UTF7.GetString(new byte[] {
(byte)b },0,1);
counter++;
Response.Write(counter);
}
stream.Close();
//****************************************************
}



Error:

The underlying connection was closed: The remote name could not be resolved.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: The remote name could not be resolved.

Source Error:


Line 107: Uri uri = new Uri("Line 108: System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(uri);
Line 109: System.IO.Stream stream = wr.GetResponse().GetResponseStream();
Line 110: int b;
Line 111: int counter=0;


Source File: c:\inetpub\ Line: 109

Stack Trace:


[WebException: The underlying connection was closed: The remote name could not be resolved.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse()
DownLoadTime.WebForm1.btnDownload_Click(Object sender, EventArgs e) in c:\inetpub\ System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
 
Not looking good for this approach since got no replies - bit of a worry
 
Did you try my code?

Also, i think that you are in the wrong forum!
 
What forum should I be in then , ya tried code similar to yours but that error will still occur ?
 
You want to 'run' a site and save its source code to a text file (maybe html), or just show the result in a console window or in a textbox. Is it supposed to be done by a web site, or it is a windows application ?
 
Its a webform that makes a request to a website ....

But I need to focus on fixing the following

[WebException: The underlying connection was closed: The remote name could not be resolved.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse()
DownLoadTime.WebForm1.btnDownload_Click(Object sender, EventArgs e) in c:\inetpub\ System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


Not much on google though. So I think I'm in the right forum as its an asp.net /c# issue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top