I am working on a form in which users are asked to provide a file's URL. I need to check if that URL really points to something. I use a CustomValidator with server-side validation. Here is the code :
Protected Sub documentUrlValide_ServerValidate
(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Handles documentUrlValide.ServerValidate
Try
Dim uri As New Uri(args.Value)
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
Dim response As HttpWebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim reader As String = New StreamReader(stream).ReadToEnd()
args.IsValid = True
Catch ex As Exception
args.IsValid = False
End Try
End Sub
I tested it with several valid URLs, none passed the test, the request.GetResponse() always throws a WebException : "can not resolve distant name".
What is wrong with this code ?
Update :
I couldn't make this work server-side, despite my code apparently being fine, so I ran it client-side with a javascript synchronous HTTP request. Here is the code (note that my application is only requested to run on IE, this code won't work on other browsers dut to Http request calls being different)
function testURLValide(sender, args)
{
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("HEAD", args.Value, false);
xmlhttp.send(null);
if (! (xmlhttp.status == 400 || xmlhttp.status == 404))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
I put your code into LINQPad and tested it out and it worked just fine...
The only difference is args.Value.
Dim uri As New Uri("http://www.google.com")
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(uri)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim reader As String = New StreamReader(stream).ReadToEnd()
reader.Dump()
<!doctype html><html><head><meta
http-equiv="content-type"
content="text/html;
charset=ISO-8859-1"><title>Google</title>............
Why not use WebClient.DownloadString() method instead?
Instead of reading the stream, how about testing for the HTTP Status Code? I believe it is response.StatusCode. The value you would look for would be 200, or possibly something in the 300s (redirect).
If you can use javascript, here's one really good way to do it: How to Test a URL in jQuery
Edit: How about one of these approaches then?
Search for "Does a URL exist?" on this page.
How to check if a URL exists in javascript
Javascript to check if URL exist
Related
using vanilla JS, i need to know if someone is using my chrome extension on a private webpage or a public webpage.
example of public webpage
https://ww.facebook.com/home
example of private webpage
https://ww.facebook.com/account/settings
Are you able to figure out if a webpage is accessible by everyone or login permissions
what i have
let xhr= new XMLHttpRequest();
xhr.open('GET', 'www.facebok.com/account/settings');
xhr.responseType = 'json';
xhr.onload = function() {
let res = xhr.state;
//res == 503?
};
xhr.send();
However, i think that since my app runs on their browser, their session will be saved and it will return a false positive.
There is no standard way of checking that for "normal" websites.
Some might in fact return a proper status code, but others (like Facebook) won't and will instead render the same 200 (OK) status page for every URL and handle the login/redirects internally via JavaScript. (This is oversimplified for the sake of this example)
You will have to write separate detection algorithms for every page you want to check.
I have this java script code on my website which gets executed when someone subscribes to my newsletter. It is basically nothing but post request. This is the piece of code.
function es_submit_request(url, parameters, es_widget_form) {
http_req = false;
http_req.onreadystatechange = function() {eemail_submitresult(es_widget_form)}; // Passing the form to the submit request
http_req.open('POST', url, true);
http_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_req.send(parameters);
}
Is is possible to call this post method from my android application code to subscribe someone to my newsletter?
I have tried this code here but it is not working.
When I debug my js code, variable values are coming as
parameters = "es_email=fgnfg#dgd.com&es_name=&es_group=×tamp=&action=0.9901232281510463"
url = "http://thetechguru.in/?es=subscribe"
I would highly appreciate if someone could help me with the code for this. I rather not use any library for this because I don't want overhead for such small thing. (for only one network call in my app)
This is the piece of code which I am trying but it is not working.
String urlString = "http://www.thetechguru.in/?es=subscribe&es_email=fsdsf#dgd.com&es_name=&es_group=×tamp=&action=0.9901232281510463";
String resultToDisplay = "";
InputStream in = null;
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception e) {
System.out.println(e.getMessage());
return e.getMessage();
}
try {
resultToDisplay = in.toString();//IOUtils.toString(in, "UTF-8");
//to [convert][1] byte stream to a string
Log.v("Response",resultToDisplay);
} catch (Exception e) {
e.printStackTrace();
}
Code executes, but nothing happens, email id is not added in list
What does you Application Manifest file look like? If your application doesn't have permission to access the internet, it won't. Try adding <uses-permission android:name="android.permission.INTERNET" /> to your AndroidMainfest.xml file (inside the <manifest> tag, but before the <application> tag.
Does anything show up in your log (log.v will only show if set to verbose)? If so, please share what it shows. Log guide
Failing that, is the es_email=fsdsf#dgd.com email already in the list? Make sure you're updating the es_email parameter of your URLstring on the button's
I have finally solved the issue. Even though I was using proper code sending POST request, I was unable to subscribe the email id. I checked the HTTP response for the request and found that it was returning error "unexpected-error". I checked in server code and there was this condition of checking HTTP_REFERER in php code. So did a little research and added REFERER in my java request and voila, success!
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//Set to POST
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
//Added referer
connection.addRequestProperty("REFERER", "http://thetechguru.in");
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setReadTimeout(10000);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(query);
writer.flush();
writer.close();
I hope it helps someone
If I have a website, how can I display data from a different website?
For example, if I have www.example.com and I want to display the sentence "I have X apples", where X is populated from www.AppleNumber.com, which I know the format of (the X I want will always be in a div named AppleNum formatted number: X )
How can I go about this?
The actual problem I want is to read from a Chrome extension's web page to see how many installations it has, but I'm certainly okay with answers to the simplified version.
I'm only adding tags I'm thinking of, don't limit your answers based on that.
I think this might solve your problem:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
From: https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
With this you can get the output of a website, like reading a txt-file.
I don't know if this will works with a Chrome extension's website.
Try
$apples = file_get_contents("http://www.AppleNumber.com/?AppleID=3");
preg_match("/<div id='AppleNum'>(\d+)<\/div>/", $apples, $Matches);
var_dump($Matches);
Regex Demo: https://regex101.com/r/uK6oR4/1
I have a simple ASP.NET web application with the following javascript that runs on an input's onblur event:
function checkUserName() {
var request = new XMLHttpRequest();
if (request == null) {
alert("Unable to create request.");
} else {
var theName = document.getElementById("username").value;
var userName = encodeURIComponent(theName);
var url = "Default.aspx/CheckName?name='" + theName + "'";
request.onreadystatechange = createStateChangeCallback(request);
request.open("GET", url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send();
}
}
The C# method this calls is the following:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string CheckName(string name)
{
return name + " modified backstage";
}
The javascript callback for the XMLHttpRequest is the following:
function createStateChangeCallback(request) {
return function () {
if (request.readyState == 4) {
var parsed = JSON.parse(request.responseText);
alert(parsed.d);
}
}
}
Although this displays the results of my server-side method, I was wondering about that property "d" I need to access to get the results. I found this only by using Intellisense. Is this property a standard property for accessing the parsed JSON? Should I be going about it some other way? Is "d" arbitrary or is it determined somehow? Is it possible for me to set the name of the property, either client or server -side?
Regarding the following:
var parsed = JSON.parse(request.responseText);
alert(parsed.d);
d is an actual property from the response object sent from the server via the GET request. It's not a special property created from the JSON.parse() method.
The server is likely wrapping the return data object into d. Thus, the response object looks something like { d: stuff } where stuff is the data that is returned.
--EDIT--
After a little digging on this, ASP.NET and WCF endpoints format the JSON object into the d property to prevent CSRF and XSS attacks. For more info, visit http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx/.
.d is added by ASP.NET in framework code under the System.Web.Extensions namespace, and it's hardcoded, so no, you can't change it unfortunately. It exists to mitigate XSS attacks. On the plus side, however, you can count on .d always being present.
Nope. Afraid not the standard xmlhttprequest object is designed for XML/HTML so you will need to parse the json from the raw text I'm afraid.
The new XHR2 spec does handle additional response types though (if your browser supports it mimd).
http://www.html5rocks.com/en/tutorials/file/xhr2/
I am making an ajax call to a service that returns html as a string with this code..
var xhReq = new XMLHttpRequest();
xhReq.open("POST", "MobileClientService.svc/REST/TestHtmlSend", false);
xhReq.send(null);
xhReq.responseType = "text/html";
var serverResponse = xhReq.responseText;
alert(serverResponse); // Shows "15"
The service creates the html correctly..
<div data-bb-type="item" data-bb-img="Images/imagesBBUI/icons/icon11.png" data-bb-title="Title From Server">
</div>
The problem is "serverResponse" in my code is returning this..
<div data-bb-type=\"item\" data-bb-img=\"Images/imagesBBUI/icons/icon11.png\" data-bb-title=\"Title From Server\">
\u000d\u000a\u000d\u000a
<\/div>
Here is the C# code used to create the html..
public string TestHtmlSend()
{
string moomoo = String.Empty;
// Initialize StringWriter instance.
StringWriter stringWriter = new StringWriter();
// Put HtmlTextWriter in using block because it needs to call Dispose.
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
//start collapse div
writer.AddAttribute("data-bb-type", "item");
writer.AddAttribute("data-bb-img", "Images/imagesBBUI/icons/icon11.png");
writer.AddAttribute("data-bb-title", "Title From Server");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//writer.Write("Some Whatever Description");
writer.RenderEndTag();
}
moomoo = stringWriter.ToString();
How do I change my code to return the html as it is without all the extra "\"?
I've run into weird issues like this before and it usually always boils down to the middleware.
Looks like a variable interpolation issue on the server side.
What is your middleware language and/or framework?
Ensure that you are printing to your output stream in a clean manner:
using only a single variable (where you can easily see quote interpolation)
not escaping on your output, unnecessarily