请帮我把这个java代码转换成C#

时间:2010-06-29 13:19:29

标签: c# java htmlunit

我在我的C#项目中使用Htmlunit,但我无法将此代码在java中转换为C#

webClient.setWebConnection(new HttpWebConnection(webClient) {
    public WebResponse getResponse(WebRequestSettings settings) throws IOException {
        System.out.println(settings.getUrl());
        return super.getResponse(settings);
    }
});

有人可以将它转换为C#吗? 提前谢谢

2 个答案:

答案 0 :(得分:0)

我不认为这可能是正确的转换,但您可以尝试并让我们知道,因为“HtmlUnit是一个Java类库,可以让您以编程方式使用网站。”

有人想要记住,虽然Java和C#之间的语法看起来相同,但声明异常的方式却有不同的处理方式。在Java中,您的类方法需要识别它可能抛出的异常,因为在C#中不是这种情况,从那里我尝试重写代码片段。

我接受的另一件事是在对象创建中定义方法,但这可能是HtmlUnit框架的一部分。

webClient.setWebConnection(new HttpWebConnection(webClient) {
  public WebResponse getResponse(WebRequestSettings settings) {
    System.out.println(settings.getUrl());
        return this.getResponse(settings);
  }
});

如果它不起作用,它可能只是在正确的方向上推动你。

答案 1 :(得分:0)

您可能正在寻找 .NET WebClient及其Download*(..) methods下的代码示例。 e.g。

using System.Net;

从MSDN复制的许多例子之一:

Console.Write("\nPlease enter a URI (for example, http://www.contoso.com): ");
string remoteUri = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Download home page data.
Console.WriteLine("Downloading " + remoteUri);                        
// Download the Web resource and save it into a data buffer.
byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);

// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDataBuffer);
Console.WriteLine(download);

Console.WriteLine("Download successful.");