2010-06-29 77 views
0

我在我的C#项目中使用的HtmlUnit,但我不能在java中把这段代码转换为C#请帮我转换这个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

'一些problem'是不是你所遇到的问题进行非常精确的描述。 – 2010-06-29 13:21:08

+0

如果你想让别人花时间,搞清楚,如何帮助你,你应该考虑花一些时间来格式化你的问题,以便它至少可读。 – folone 2010-06-29 13:22:38

+0

我试过在提交表单之后提交表单,如上面的代码 – user379017 2010-06-30 08:05:54

回答

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

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

+0

谢谢你的回答,但是这段代码不工作。我正在继续寻找任何解决方案。此代码让我做恶梦 – user379017 2010-06-30 02:29:19

0

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

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."); 
+0

谢谢,但在此代码中,我希望从htmlunit发布时获取请求标题 – user379017 2010-06-30 07:07:50