So, I know it's possible to ping a server, with Ping.Send(string) in c#, but would it be possible to ping a specific web application, instead of the whole server. For example, there is a server with three websites (a, b, and c) hosted on it. The server IP is 1.1.1.1. Each of the websites has a different port. How would I check to see if website a is currently being hosted?
Just make a request to WebSite
private bool PingWebSite(string url)
{
try
{
WebRequest.Create(url).GetResponse();
return true;
}
catch
{
return false;
}
}
And then use it
var isWebSiteWorking = PingWebSite("http://stackoverflow.com");
I wouldnt do the GetResponse() because, what if that particular Url is returning you a 1+ GB of file, this will block your application. Just making the head request should be sufficient or using TcpClient.
async Task<Boolean> IsAvailable()
{
string url = "http://www.google.com";
try
{
using (var client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, url);
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
response.Dump();
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
return false;
}
}
Related
I'm currently developing an app that uses MVC Identity where users can register and whilst doing so they add URLs to the database.
I would like to know the best way to include a script that scans the database and 'flags'/'reports' any broken URLs so they can be manually removed from the database every 2 weeks.
I have found one online in PHP but I would ideally like to do this using C#.
Does anyone know how I would go about doing this?
Thanks
there are a lot of ways to implement such a code.
all we need to do is to try to download the file , and if its not there( AKA Link broken) it will get exception and return false.
here is an example:
public bool checkWebsite(string URL) {
try {
WebClient wc = new WebClient();
string HTMLSource = wc.DownloadString(URL);
return true;
}
catch (Exception) {
return false;
}
}
and usage:
//The checkWebsite command will return true:
bool websiteExists = this.checkWebsite("https://www.google.com");
bool websiteExists2 = this.checkWebsite("https://www.beucbeiucbeuiceiuhc.com/nonExist");
Another FASTER way of performing the test is by trying HEAD request instade of GET request. in this way you can improve the speed of the testing.
Here is some example for that method:
class MyClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
req.Method = "HEAD";
return req;
}
}
public bool checkWebsite(string URL) {
try {
MyClient wc = new MyClient();
string HTMLSource = wc.DownloadString(URL);
return true;
}
catch (Exception) {
return false;
}
}
How to Verify if the download uri is valid without causing client to download content?
create client
Use client.GetAsync(uri, HttpComplettionOption) api
use ResponseHeadersRead
this will only check if the uri is valid without downloading actual content.
HttpClient client = new HttpClient();
var downloadUri = new Uri("https://domainame/someblobcontent.zip");
Task<HttpResponseMessage> response = null;
try
{
using (response = client.GetAsync(downloadUri, HttpCompletionOption.ResponseHeadersRead))
{
if(response.Result.IsSuccessStatusCode)
{
Console.WriteLine("uri is valid, got response code {0}", response.Result.StatusCode);
}
else
{
Console.WriteLine("uri is not valid, got response code {0}", response.Result.StatusCode);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
hello I am trying to launch an intent with a webview from a user entered URL, I have been looking everywhere online and I can't find a concrete answer as to how to make sure the website will actually connect before allowing the user to proceed to the next activity. I have found many tools to make sure the URL follows the correct format but none that actually let me make sure it can actually connect.
You can use WebClient and check if any exception is thrown:
using (var client = new HeadOnlyClient())
{
try
{
client.DownloadString("http://google.com");
}
catch (Exception ex)
{
// URL is not accessible.
}
}
You can catch more specific exceptions to make it more elegant.
You can also use custom modification to WebClient to check HEAD only and decrease the amount of data downloaded:
class HeadOnlyClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
req.Method = "HEAD";
return req;
}
}
I would suggest you to use HttpHead for simple request with AndroidHttpClient, but it is deprecated now. You can try to implement HEAD Request by sockets.
You can try to ping the address first.
See this SO question: How to Ping External IP from Java Android
Another option:
Connectivity Plugin for Xamarin and Windows
Task<bool> IsReachable(string host, int msTimeout = 5000);
But, any pre-check that succeeds isn't guaranteed as the very next request might fail so you should still handle that.
Here's what I ended up doing to Check if a Host name is reachable. I was connecting to a site with a self signed certificate so that's why I have the delegate in the ServiceCertificateValidationCallback.
private async Task<bool> CheckHostConnectionAsync (string serverName)
{
string Message = string.Empty;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serverName);
ServicePointManager.ServerCertificateValidationCallback += delegate
{
return true;
};
// Set the credentials to the current user account
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "GET";
request.Timeout = 1000 * 40;
try
{
using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync ())
{
// Do nothing; we're only testing to see if we can get the response
}
}
catch (WebException ex)
{
Message += ((Message.Length > 0) ? "\n" : "") + ex.Message;
return false;
}
if (Message.Length == 0)
{
goToMainActivity (serverName);
}
return true;
}
I am trying to send request to http://localhost/apptfg/get_biography_group?nombre_grupo=fondoflamenco which is a php based webservice that access to mysql database and retrieve information about the group but I always get a 404 not found when I execute the application in my windows phone 8 device, however when I debug the url in fiddler I get the right result which must be {"success":1,"group":[{"nombre_grupo":"fondoflamenco","anyo_creacion":"2006","descripcion":"Fondo Flamenco Flamenco is a group formed by three young Sevillian. Astola Alejandro Soto, Antonio Sanchez and Rafael Ruda M.R","musicos":"Rafael Ruda,Antonio Manuel Rios,"}]}
this is the HttpClient code I use in my application:
public async Task<string> makeHttpRequest(string group_name)
{
var resultstring = String.Empty;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "text/html");
try
{
resultstring = await client.GetStringAsync(new Uri("http://localhost/apptfg/get_group_biography.php?nombre_grupo=" + group_name));
client.Dispose();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
return resultstring;
}
Problem: This console app calls a long running webpage hosted on Azure twice. I want it to call it only once.
The console app fails with a caught exception: The underlying connection was closed: An unexpected error occurred on a receive. so question
If I call the page from Chrome, it runs once (as expected)
public class ExtendedWebClient : WebClient
{
public int Timeout { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
if (request != null)
request.Timeout = Timeout;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
return request;
}
public ExtendedWebClient()
{
Timeout = 1000000; // in ms.. the standard is 100,000
}
}
class Program
{
static void Main(string[] args)
{
var taskUrl = "http://secret.net/SendWeeklyEmails.aspx";
// create a webclient and issue an HTTP get to our url
try
{
using (ExtendedWebClient httpRequest = new ExtendedWebClient())
{
var output = httpRequest.DownloadString(taskUrl);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception was: " + ex.Message);
}
}
}
Simple answer - I don't believe this client calls the page twice!
If your call is long running and Azure doesn't allow you to do long-polling, then you will need to rearchitect this app so that you have separate calls for starting and then progress monitoring this "SendWeeklyEmails" task. You could even do this using your command line client code, instead of using the web app.