0

我在我的 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# 吗?预先感谢

4

2 回答 2

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

如果它不起作用,它可能只会将您推向正确的方向。

于 2010-06-29T21:21:13.710 回答
0

您可能正在寻找.NET WebClient及其Download*(..) 方法下的代码示例。例如

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.");
于 2010-06-30T02:36:22.913 回答