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?
Related
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);
My Code for Downloader uses two textboxes :
Two get user URL
To specify the drive
private void downloadbtn_Click(object sender, EventArgs e)
{
WebClient myWebClient = new WebClient();
//Declarations for string objects
string downloadURL, path;
//raw URL taken from user
downloadURL = this.downloadURL.Text;
path = savePath.Text;
Uri tmp = new Uri(downloadURL);
string EndPathFileName = tmp.Segments.Last();
path = path + #"\" + EndPathFileName;
//downloads file using async method
myWebClient.DownloadFileAsync(tmp, savePath.Text);
}
The issue arises when i use URL of type:
http://www.dailymotion.com/video/x1coxfe_aryan-khan-tera-pyar-official-music-video-hd_music
which didn't have any suffix i.e. mp3,mp4 etc.
Here it downloads an icon of zero data in the winkle of eye as compared to url having *.mp3/4 etc
Any suggestions please
You should check the Content-Type header not the URL to know the type of file. You can read the content-type like this
string type = client.ResponseHeaders["content-type"];
I have files on a server that can be accessed from a URL formatted like this:
http:// address/Attachments.aspx?id=GUID
I have access to the GUID and need to be able to download multiple files to the same folder.
if you take that URL and throw it in a browser, you will download the file and it will have the original file name.
I want to replicate that behavior in C#. I have tried using the WebClient class's DownloadFile method, but with that you have to specify a new file name. And even worse, DownloadFile will overwrite an existing file. I know I could generate a unique name for every file, but i'd really like the original.
Is it possible to download a file preserving the original file name?
Update:
Using the fantastic answer below to use the WebReqest class I came up with the following which works perfectly:
public override void OnAttachmentSaved(string filePath)
{
var webClient = new WebClient();
//get file name
var request = WebRequest.Create(filePath);
var response = request.GetResponse();
var contentDisposition = response.Headers["Content-Disposition"];
const string contentFileNamePortion = "filename=";
var fileNameStartIndex = contentDisposition.IndexOf(contentFileNamePortion, StringComparison.InvariantCulture) + contentFileNamePortion.Length;
var originalFileNameLength = contentDisposition.Length - fileNameStartIndex;
var originalFileName = contentDisposition.Substring(fileNameStartIndex, originalFileNameLength);
//download file
webClient.UseDefaultCredentials = true;
webClient.DownloadFile(filePath, String.Format(#"C:\inetpub\Attachments Test\{0}", originalFileName));
}
Just had to do a little string manipulation to get the actual filename. I'm so excited. Thanks everyone!
As hinted in comments, the filename will be available in Content-Disposition header. Not sure about how to get its value when using WebClient, but it's fairly simple with WebRequest:
WebRequest request = WebRequest.Create("http://address/Attachments.aspx?id=GUID");
WebResponse response = request.GetResponse();
string originalFileName = response.Headers["Content-Disposition"];
Stream streamWithFileBody = response.GetResponseStream();
I want to get HTML code to be displayed in a RichTextBox. I am using the code
WebClient client = new WebClient();
byte[] data = client.DownloadData("http://www.google.com");
richTextBox1.Text = data.ToString();
How can I do this?
Also: I don't know why but this shows me "System.Byte[]" on the RichTextBox.
Use WebClient.DownloadString that downloads the specified resource as a String or a Uri:
var contents = new System.Net.WebClient().DownloadString(url);
Note that: RTF encoding is different from HTML. You cannot do this straight away. I suggest WebBrowser control.
or try this ways:
http://www.codeproject.com/KB/HTML/XHTML2RTF.aspx
http://www.codeproject.com/KB/edit/htmlrichtextbox.aspx
It shows System.Byte[] Because it is show the description of data, not data's contents. to do this do something like:
WebClient client = new WebClient();
byte[] file = client.DownloadData("example.com");
File.WriteAllBytes(#"example.txt", file);
string[] lines = File.ReadAllLines("example.txt");
richTextBox1.Text = lines;
To see the actual content
EDIT
Or you can do WebClient.DownloadString like #Ria Suggested. Only I would implement it like this:
WebClient client = new WebClient();
var data = client.DownloadString("example.com");
richTextBox1.Text = data.ToString();
Or to be more efficient even
richTextBox1.Text = client.DownloadString("example.com");
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");