I need to load a webpage, but I need the redirects to work (there can be some redirects using JavaScript instead of just proper HTML 3xx code). I can't make it work. I don't even need to load the content - I just have to make a hit there, so the server can count the page was loaded.
I've already tried WebClient:
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone; Trident/6.0; IEMobile/10.0)";
wc.DownloadStringAsync(new Uri(myUrl), wc);
as well as HttpWebRequest:
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(myUrl);
request.Method = "POST";
request.AllowAutoRedirect = true;
request.BeginGetResponse(null, null);
but both of them get only to first page.
I also tried using "hiden" WebBrowser:
WebBrowser wb = new WebBrowser();
wb.Visibility = Visibility.Collapsed;
wb.IsScriptEnabled = true;
wb.Navigate(myUri);
but it doesn't even get to the first page.
There's no way you can do that with WebClient or HttpWebRequest (since they don't understand Javascript), but the WebBrowser should work (I'm already doing that in one of my apps). Just make sure to add it to your page (even if hidden), and call the Navigate method from the UI thread.
Related
I am working on a project where I need to scrap data in JSOUP and the show as HTML in my webpage and on clicking view more I am forwarding to the scrapped URL, issue is where GET url's are working fine but POST request for aspx are not working as it expects some __EVENTVALIDATION, etc,.. as input in form.
The webpage is kepler.sos.ca.gov, if you click on Corporation Name then enter ESCROW, then submit, some results will come up, which I am able to do in JSOUP, but unable to redirect using javascript:__doPostBack('ctl00$content_placeholder_body$SearchResults1$GridView_SearchResults_Corp','DetailCorp$0').
Please advice.
There is a answer over here : unable to get results from jsoup while giving post request
`Connection.Response response = Jsoup.connect(url)
.method(Connection.Method.GET)
.execute();
Document responseDocument = response.parse();
Map<String, String> loginCookies = response.cookies();
Element eventValidation = responseDocument.select("input[name=__EVENTVALIDATION]").first();
String validationKey = eventValidation.attr("value");
Element viewState = responseDocument.select("input[name=__VIEWSTATE]").first();
String viewStateKey = viewState.attr("value");
response = Jsoup.connect(url)
.cookies(loginCookies)
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT", "")
.data("__LASTFOCUS", "")
.data("__VIEWSTATE", viewStateKey)
.data("__VIEWSTATEENCRYPTED", "")
.data("__EVENTVALIDATION", validationKey)
.data("ctl00$content_placeholder_body$BusinessSearch1$TextBox_NameSearch", "aaa") // <- search
.data("ctl00$content_placeholder_body$BusinessSearch1$RadioButtonList_SearchType", "Corporation Name")
.data("ctl00$content_placeholder_body$BusinessSearch1$Button_Search", "Search")
.method(Connection.Method.POST)
.followRedirects(true)
.execute();
Document document = response.parse(); //search results
System.out.println(document);`
JSoup is not a browser. It does not interpret JavaScript and you can't fire a POST request through a JavaScript link.
JSoup is however quite capable doing POST requests. To use JSoup here, you need to figure out how the actual request is built by the JavaScript. You then can run the same algorithm coded in Java and create the link and do the POST request.
An easier way of achieving what you want is maybe a switch in technology. You can use selenium for example, which allows to "remote control" a real browser, which should have no problem running the JavaScript of that page.
In my windows phone 8 application, When I call the web service I'm getting the response. And the response is html code. Now I have stored the html code in string variable. Now I want to show this html code in the web browser. For this I've used the below code.
myWebBrowser.NavigateToString("myHTMLcode")
But the data is not showing in the page. Please tell me how to do the above. I'm looking forward for valuable response.
Thanks.
Create a WebBrowser control in XAML (make sure it has enough height and width set)
<phone:WebBrowser Name="webBrowser" Height=400 Width=300/>
Then I created a web client to fetch some html page
WebClient client = new WebClient();
client.DownloadStringCompleted += client_DownloadStringCompleted;
client.DownloadStringAsync(new Uri("http://www.google.com", UriKind.Absolute));
When the html is downloaded, show it in our webBrowser control
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
webBrowser.NavigateToString(e.Result);
}
Adopt this to your needs.
I have a webpage that allows users to download excel reports, in Chrome, it just tries to download an aspx page instead of the excel like it successfully does in IE and Firefox.
Is this a known issue and is there a workaround?
I am using this in VB:
db.subRunReader(resultSQL)
dgProperties.DataSource = db.sqlReader
dgProperties.DataBind()
db.subCloseReader()
db.subCloseConnection()
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dgProperties.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
Response.Redirect(Request.Url.ToString())
Thanks.
Not sure about your specific situation, but in general whenever you want a file downloaded, it is a good idea to add a Content-Disposition header. In ASP.NET MVC it would be done like this:
Response.Headers["Content-Disposition"] =
"attachment;filename=yourfile.xls";
I expect in ASP.NET 2.0 it would be something similar. This tells the browser that it needs to download the file.
Trying to get the current URL of the website I have posted the flash object in, without using Javscript or ExternalInterface calls.
e.g.
Website Im posting in is http://some.forum.com
Website of swf is http://uploaded.to/site/of/object.swf
In my post at some.forum.com I include in the post:
[flash=100,100]http://uploaded.to/site/of/object.swf[/flash]
Unfortunately it keeps returning the url of website I initially uploaded to, and not the current forum I am linking it in.
Current used methods are:
var urlname:LocalConnection = new LocalConnection();
trace(urlname.domain());
and
var urlName:String = this.loaderInfo.url;
trace(urlName)
Both returning uploaded.to/site/of/object.swf, and not some.forum.com
How do I get my desired result?
Not sure if it applies to your scenario but have you tried BrowerManager: http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf64e50-7ff8.html
Something like
var browserManager:BrowserManager = BrowserManager.getInstance();
trace(browserManager.url);
Hmm. Well I've found a small workaround: Referrer.
All I need to do is get the swf to request another website that performs some javascript or php to return the URL of the referrer website.
var textLoader:URLLoader = new URLLoader()
var textReq:URLRequest = new URLRequest("http://my.site.com/findReferrer.html");
textLoader.load(textReq);
textLoader.addEventListener(Event.COMPLETE, trace(textLoader.data);
Unfortunately referrer is purely browser dependent and often doesn't work for https.
So back to square one..
I would like to display the image from a network camera on my web page, but the image is behind a HTTP basic authentication server.
In Firefox and Chrome I can do this:
<img width="320" height="200" src="http://username:password#server/Path" />
But in Internet Explorer 8, I get an empty image box. If I use JQuery to set the src attribute, IE8 displays a blank src. It looks like IE8 is checking the string and rejecting it.
Is there a way to put the basic authentication credentials in the img tag?
Bottom line: Not all browsers allow this. It may work in some but not others.
But as someone else has said already, it's not very safe -- you're effectively giving the login and password details to anyone who browses the page. Not good.
A better option would be proxy it through the same server that you're providing the html code from, then the href in the <img> tag could just be a local URL, and no-one need know where the image is actually coming from.
You can load your img with AJAX, using XMLHttpRequest. As you might know, XMLHttpRequest has a setRequestHeaders method, so you will be able to manipulate headers for your request, hence, you will be able to do basic HTTP authentication.
Best way is to create a login page, then setup port forwarding on your router to display the camera. Does the camera come with web software?
See http://support.microsoft.com/kb/834489 and http://msdn.microsoft.com/en-us/library/ie/ee330735(v=vs.85).aspx#login_creds_urls
Long story short, usernames/passwords are not formally supported under the HTTP/HTTPS protocol schemes and due to their use in phishing attacks, they were disabled in IE6 on XPSP2. Support can be manually re-enabled by the user using a registry override (FEATURE_HTTP_USERNAME_PASSWORD_DISABLE) but this is not recommended.
Try http proxy.
On server side, enable tinyProxy, create ReversePath to
basic authentication server in configuration like:
AddHeader "Authorization" "Basic dXNlcjpwYXNz"
ReversePath "/foo/" "http://somewhere:3480/foo/"
dXNlcjpwYXNz is base64 encoded string from user:pass
Enable reverse proxy in Apache or NGINX to tinyProxy path http://localhot:8888/foo/
Img Source accessable from local server instead of old way deprecated, without http auth pop-up or CORS error.
http://user:pass#somewhere:3480/foo/DEST.jpg
ajax add http header works!
pictureUrl = "https://somewhere/file.jpg";
var oReq = new XMLHttpRequest();
oReq.open("GET", pictureUrl, true);
oReq.setRequestHeader("Authorization", "Basic " + btoa("UserName"+":"+"Password"));
// use multiple setRequestHeader calls to set multiple values
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
var u8 = new Uint8Array(arrayBuffer);
var b64encoded = btoa(String.fromCharCode.apply(null, u8));
var mimetype="image/jpeg"; // or whatever your image mime type is
document.getElementById("iOdata").src="data:"+mimetype+";base64,"+b64encoded;
}
};
oReq.send(null);