Load a web page to send data - c#

Assume that I have a php script which saves get queries on info parameter. I have it as a string variable:
string url = "http://example.com/write.php?info="
Let's say I have a string like this:
string info = "asd";
So I want to load the web page without showing to user. The url will be url+info
How can i do that?

You can use WebClient:
var fullUrl = url + info;
var conent = new System.Net.WebClient().DownloadString(fullUrl);

Related

Replace part of URL using

I have a url that looks like this
https://domain1.com/go/2345/Default.aspx?c%7c2vCZVIjuUzLTfgsgagasgsgasgsagag
I would like to be able to replace the domain1.com for domain12.com so it would look like this
https://domain12.com/go/2453545/Default.aspx?
How I can replace only the domain1.com part? Quick note : Everything after the "go/"changes every time I open the browser
I try this
I get the Url from the browser
string getUrl = Url;
then I replace the value
string newUrl = getUrl .Replace(getUrl .Substring(url.IndexOf(go)
var u = "https://domain1.com/go/2345/Default.aspx?c%7c2vCZVIjuUzLTfgsgagasgsgasgsagag";
var uri = new Uri(u);
var path =
uri.PathAndQuery.Substring(0, uri.PathAndQuery.Length - uri.Query.Length);
string newUrl = "https://domain2.com" + path;
Console.WriteLine(newUrl);
// OUTPUT: https://domain2.com/go/2345/Default.aspx

How to download a file from a URL which is passed as a query string?

I am passing a URL which has another URL in the query string. I have shown an example below:
https://www.aaa.com/triBECKML/kmlHelper.htm?https://lkshd.ty.etys.nux/incoming/triBEC/final_year_base_data/KMLS/NetAverages.kml
I have tried a WebClient to download the file but it only downloads an empty .kml. Additionally when I call the method with just the 2nd URL (IN THE QUERY STRING), the file gets downloaded smoothly.
using (var client = new WebClient())
{
client.DownloadFile(url, destinationPath);
}
You can try to split the string. And use only the second part?
Example:
string[] urls = url.Split('?');
using (var client = new WebClient())
{
client.DownloadFile(urls[1], destinationPath);
}
This splits the url string and puts the first url and the second in a array. Then you can use the url you want.

Using c#, ios to get a JSON address from an API

I am trying to make for example a simple weather app. Mine is for an Air Quality API.
I had this code but I didn't think it would work with JSON.
var webClient = new WebClient();
...
var text = e.Result; // get the downloaded text and store in this variable
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = "downloaded.txt"; // local file to save text in
string localPath = Path.Combine(documentsPath, localFilename); // local path to save file to
File.WriteAllText(localPath, text); // writes to local storage
myTextView.Text = text; // updates the TextView element on the screen with downloaded text data
Then Just
var url = new Uri("https://api.breezometer.com/baqi/?lat=" + latitude + "&lon=" + longitude +"&key=YOUR KEY HERE");
webClient.Encoding = Encoding.UTF8;
webClient.DownloadStringAsync(url);
Im just not sure of the encoding method for JSON.
If anyone knows please answer thanks...
Use the WebClient class in System.Net:
var json = new WebClient().DownloadString("url");
Origin answer:
How to get a json string from url?

webclient or httpwebrequest to retrieve hrefs and url

How do I use either webclient or httpwebrequest to do two things:
1)Say after downloading the resource as a string using:
var result = x.DownloadString("http://randomsite.com);
there's a relative url(also query string):
Click here to get your name and age
how do I click(follow) on that link using webclient? after initially loading the resource in result. i was able to use htmlagilitypack to isolate the href but I would now like to follow it in code.
2) If the httpwebrequest does not redirect but instead loads the same page with different parameters how would i use webclient to retrieve the new url that is generated?
i.e if i call
var result = x.DownloadString("http://randomsite.com);
but this actually calls
http://randomsite.com/q?site=default
I then want to retrieve the second url
Thanks in advance
You can construct the url from the link and the link that you just downloaded like this:
Uri baseUri = new Uri("http://randomsite.com");
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
Console.WriteLine(myUri.ToString()); // gives you http://randomsite.com/q?name=john&age=50
This also works if you base Url has url parameters.
As for the second question, i guess you meant that the request was redirected and you want that url instead? Then the easiest way to do so is to sub-class WebClient described here.
Uri baseUri = new Uri("http://randomsite.com");
using(var client=new WebClient())
{
var result = client.DownloadString(myUri);
//get href via HtmlAgilityPack...
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
result = client.DownloadString(myUri);
}

uploading a video file to Facebook with description and action links

In C#, I'm uploading a video file to Facebook from a server, using the following method:
string fullurl = "https://graph-video.facebook.com/me/videos?" + "title=" + title + "&access_token=" + accessToken;
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, path);
path is the path to the video file on the server. This works, and shows a video uploaded post on the user profile. How can I add a text description (with link) and action links to that post?
please keep the answers in C#
you should pass extra query string parameter - description - with embedded URL. Something like this:
var description = "Bring your conversations to life on Facebook. With face-to-face video calling, now you can watch your friends smile, wink and LOL.\n\nTo get started, visit http://www.facebook.com/videocalling";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode(title), HttpUtility.UrlEncode(description ), accessToken);
I did it. Following is my code:
string a = "User_Access_Token";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode("Dulha"), HttpUtility.UrlEncode("hello"), a);
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, #"C:\Users\Users\Downloads\Dulha.mp4");

Categories

Resources