Set width or resolution for HtmlUnit web client - htmlunit

Is it possible to set the width or screen resolution of the emulated browser in a HTMLUnit WebClient?
After some Googling, I did find a class in HtmlUnit called WebWindow, which contains methods such as setOuterWidth, but I am not sure how to use these methods.
is it possible?

Use WebClientOptions like this:
WebClient webClient = new WebClient();
webClient.getOptions().setScreenHeight(1024)

Related

Using HttpClient to make a call within a Web API

I've been working on a .NET 4.6.1 Web API project. As part of this project, I need to call another Web API and I want to use the HttpClient to do so.
From my research online, you can't rely on just doing a normal HttpClient within a using clause as it doesn't garbage collect correctly and can lead to memory leaks.
E.g., I'm currently using it as follows:
using (HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(CONTENTFUL_TIMEOUT_IN_SECONDS) } )
{
responseText = await client.GetStringAsync(uri).ConfigureAwait(continueOnCapturedContext:false);
}
But as suggested in other articles from Stack Overflow and others, this leads to memory leaks, and the way around this is to share a single instance of the HttpClient.
E.g., check HTTPCLIENT DESTABILIZING YOUR SOFTWARE and HttpClientHandler/HttpClient Memory Leak.
I'm not sure however how to setup a shared "single" instance of the HttpClient from within an WebAPI itself?
You should have a look on how to implement singleton pattern. Refer to this.
Then you can create a singleton of HttpClient and make it responsible for all HTTP calls from your API.

Adding IE browser compatibility to HtmlUnit

There is an application which works only in IE, not with any other browsers. I am using HtmlUnit to send the request by setting the mandatory field and clicking on the submit button, which is giving me Error Message -
An internal error has occurred during processing
Though i have specified browserversion while creating the webClient object, like this
WebClient webClient = null;
BrowserVersion bv = BrowserVersion.INTERNET_EXPLORER_11;
webClient = new WebClient(bv);
Can anybody please tell me what else am i missing here ?
Please let me know if there is any other way of getting this done here using HtmlUnit.

Custom USER-AGENT in UWP WebView

I'm working on a UWP app and have a page with a WebView. In the WebView I need to set the user-agent to a custom value.
I have tried the following:
var requestMessage = new HttpRequestMessage(HttpMethod.Get, baseUri);
requestMessage.Headers.Add("User-Agent", "MyCustomValue");
webview.NavigateWithHttpRequestMessage(requestMessage);
However the WebView doesn't use my custom user-agent but instead use the original default value of the user-agent. This is confirmed by this thread at MSDN.
Any good input to alternative solutions or workarounds is appreciated.
It seems only to be supported when doing POST, not GET.
Perhaps this blog post can get you closer to a solution: https://basquang.wordpress.com/2014/04/26/wp8-1-changing-windows-phone-8-1-webview-default-user-agent-in-all-outbound-http-requests/
Try it:
var rm = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, new Uri("https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending"));
rm.Headers.Add("User-Agent", "test");
rm.Headers.Add("NSASESSIONID", "CA79AB9B-21CD-43BE-A48A-49B5F1289D22");
WebView.NavigateWithHttpRequestMessage(rm);
It's working for me.

HtmlUnit and saving page as PDF

In the Java EE environment, I want to load an XML and XSL file, render the output to a browser window, and save the rendered page as PDF.
I would like to do it all programmatically. I was looking at HtmlUnit to use as a headless browser. That part seems to work, but does HtmlUnit have any API to invoke a "print" function or similar function to persisted the rendered output? I was thinking of some way to link it in with iText.
I would recommend trying out flying-saucer which in this case basically is a CSS/XHTML enabled iText wrapper.
I wrote a simple example below. Don't forget the necessary dependencies for HtmlUnit and flying-saucer.
//Set up a new WebClient using your favourite settings
WebClient webClient = new WebClient();
//Fetch page
HtmlPage page = webClient.getPage("url-to-target.resource");
//Set PDF target output file
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
//Set up flying-saucer IText based renderer
ITextRenderer renderer = new ITextRenderer();
//Create PDF
renderer.setDocumentFromString(page.asXml();
renderer.layout();
renderer.createPDF(os);
os.close();
If you're doing reporting in Java i highly recommend you to use Jasper Reports, it's relatively easy to use, you can design the report graphically and it can take XML as input and give you a report in many formats (including PDF).

How do you disable caching with WebClient and Windows Phone 7

I am making a call to a REST web service and the mobile app is retrieving the results from its cache and not going to the server.
I have seen other suggested fixes (similar issue and similar issue2) but the Cache property is not available in silverlight 4.
Does anyone have an idea of how to force silverlight 4 on windows phone 7 to make a request and not hit the cache?
Although not ideal, a easy solution is to send something like the field "junk" with the value DateTime.Now. That way, a value is always brand new, and will never get cached. If you were doing this in a standard querysting for example:
"&junk=" + DateTime.Now;
I've hit this problem too on overflow 7 talking to StackApps - the only thing I could think of was to add an addition random variable to the end of the HTTP/REST request.
The most proposed solution is the same as William Melani's.
But it is not ideal and some services reject requests with unknown parameters or any parameter. In this case it is cleaner and more reliable to use the IfModifiedSince header as follows:
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri(bitstampUrl));
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
worked for me

Resources