tags:

views:

128

answers:

2

I'm using Htmlunit in my C# project, but I cannot convert this code in java to C#

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

Can anyone kindly convert it to C# ? Thank in advance

A: 

I don't think that this might be the right conversion, but you can try it and let us know, as "HtmlUnit is a class library for Java that lets you programmatically play with web sites."

One think to remember that although the syntax between Java and C# looks the same, the way exceptions are declared is treated differently. In Java your class methods needs to identify the exceptions that it could possibly throw, where as this is not the case in C#, from there my feeble attempt to rewrite the piece of code.

Another thing I've picked up is the defining of methods within the object creation, but that might be part of the HtmlUnit framework.

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

If it is not working, it might just nudge you in the right direction.

Riaan
Thanks your answer, but this code not working. I'm continue looking for any solution. This code make me nightmares
A: 

You might be looking for the .NET WebClient class and the code examples under its Download*(..) methods. e.g.

using System.Net;

One example of many, copied from 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.");
John K
Thanks, but in this code I want to get the request header when post from htmlunit