How does one load a URL from a .NET client application - c#

What is the preferred way to open a URL from a thick client application on Windows using C# and the .NET framework? I want it to use the default browser.

The following code surely works:
Process.Start("http://www.yoururl.com/Blah.aspx");
It opens the default browser (technically, the default program that handles HTTP URIs).

I'd use the Process.Start method.

private void launchURL_Click(object sender, System.EventArgs e){
string targetURL = "http://stackoverflow.com";
System.Diagnostics.Process.Start(targetURL);
}

System.Diagnostics.Process.Start("http://www.stackoverflow.com");

Related

Moved Permanently error when calling a PHP WebService from .NET

I have developed a WebService in PHP... it works perfectly when I call it from a PHP application, but I have problems when calling from .NET.
The error I am received when I call a web method is "Moved Permanently".
The WSDL of the service is at: http://feriados.servicios.desytec.com/feriados?wsdl
And the .NET code is the following:
private void button1_Click(object sender, EventArgs e)
{
Servicio.Desytec_Feriados svc = new Servicio.Desytec_Feriados();
var feriados = svc.GetHolidays("8cd4c502f69b5606a8bef291deaac1ba83bb9876", "cl", "2014", "5");
System.Diagnostics.Debug.WriteLine(feriados.Length);
}
It was due because the URL of the WSDL was bad specified. It was written as http://destec.com/app.php/feriados, but the correct is http://destec.com/feriados, because .htaccess is stripping the app.php part (the webservice was programmed using Symfony PHP framework).
Regards
Jaime

WP7 WebClient cannot find specific server

Ok, maybe this is the dumbest question ever but I really can't understand what's happening :D
I have this simple code:
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri url = new Uri("http://www.something.com/");
WebClient wc = new WebClient();
wc.DownloadStringAsync(url);
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}
It works well, except for one case (obviously the one that I need), when the address of the server is "ts4.travian.it". In this case I get this error: "The remote server returned an error: NotFound."
The strange thing is that if I write a small console application with basically the same code, it works...any idea?
EDIT: To be more specific, the server return error 403 - Forbidden, but if I try the same code in a Console Application, it works perfectly...don't know what to think..I'm debugging the application on a Nokia Lumia 800
I found the problem finally. The Silverlight framework automatically set the Referer header of the HttpWebRequest, and the server "ts4.travian.it" refused it because it was not correct.
Did you forget to provide the output for that one case?
Edit:
Status code '403' implies that there' something the server doesn't like in your app. Who knows maybe it checks your UA string and sees that you're using IE Mobile i guess. Than he desides not to allow you see the content. Hm?

How do I utilize Webclient.DownloadFile or DownloadString if the website uses SSL?

I'm writing a small utility that scrapes a webpage and emails me if the class I want has open seats. See the follow url:
Math Classes
However, I get an error everytime I execute the following code:
private void timer1_Tick(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
//var html = client.DownloadString("https://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US");
client.DownloadFile(#"https://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US", #"C:\file.txt");
}
}
Any idea?
That link you posted also supports simple HTTP.
See here: http://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US
Try removing your s from the url and see if that works.

How to use WebClient.DownloadData(to a local DummyPage.aspx)

I'm following a tutorial on this link http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
Now I'm stuck at these codes
private const string DummyPageUrl =
"http://localhost/TestCacheTimeout/WebForm1.aspx";
private void HitPage()
{
WebClient client = new WebClient();
client.DownloadData(DummyPageUrl);
}
My local application address has a port number after "localhost", so how can I get the full path (can it be done in Application_Start method)? I want it to be very generic so that it can work in any cases.
Thanks a lot!
UPDATE
I tried this in the Application_Start and it runs fine, but return error right away when published to IIS7
String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
If it is calling back to the same server, perhaps use the Request object:
var url = new Uri(Request.Url, "/TestCacheTimeout/WebForm1.aspx").AbsoluteUri;
Otherwise, store the other server's details in a config file or the database, and just give it the right value.
But a better question would be: why would you talk via http to yourself? Why not just call a class method? Personally I'd be using an external scheduled job to do this.
You need an answer that works when you roll out to a different environment that maybe has a virtual application folder.
// r is Request.Url
var url = new Uri(r, System.Web.VirtualPathUtility.ToAbsolute("~/Folder/folder/page.aspx")).AbsoluteUri;
This will work in all cases and no nasty surprises when you deploy.
I suspect you're using the ASP.NET development server that's built-in to Visual Studio, which has a tendency to change port numbers by default. If that's the case, then you might try simply configuring the development server to always use the same port, as described here. Then just add the port number to your URL, like so:
private const string DummyPageUrl =
"http://localhost:42001/TestCacheTimeout/WebForm1.aspx";

How to transform ASP.NET URL without URL routing or URL rewrite?

I am using ASP.NET 2.0 on IIS6 therefore I can’t use system.web.routing. I’ve tried a few URL rewriters but none did what I wanted.
I basically need to transform (read/parse then redirect) URL 1 into 2 with minimum IIS configuration because I don't have the full authority to reconfigure web servers (i.e. ISAP on IIS6 or install 3rd party extensions/libraries). And I can’t transform URL into 3 because all the physical links will break.
http://www.url.com/abc123
http://www.url.com/default.aspx?code=abc123
http://www.url.com/abc123/default.aspx?code=abc123
Thank you!
Create 404 error handler, e.g. 404.aspx. In that page, parse request URL and extract code using Request.Path. Then, redirect to default.aspx?code=abc123.
You should be able to set 404 (page not found) handler to 404.aspx for your website, most hosting providers allow that.
I would suggest that you look at using a custom transform with UrlRewriter.net. This link details how you can do it with the Intelligencia.UrlRewriter assembly (about 3/4 of the way down the page). Hopefully is good enough to do what you need.
protected void Page_Load(object sender, EventArgs e)
{
HtmlLink canonicalTag = new HtmlLink();
canonicalTag.Href = "http://www.url.com/default.aspx";
canonicalTag.Attributes["rel"] = "canonical";
Page.Header.Controls.Add(canonicalTag);
}
http://codersbarn.com/post/2009/02/21/ASPNET-SEO-and-the-Canonical-Tag.aspx

Categories

Resources