Check if connect to remote server by Webclient - c#

I want to check if my project can connect to the remote server by Webclient in ASP.NET C# and do something.
here is my code
WebClient webClient = new WebClient();
webClient.Credentials = new System.Net.NetworkCredential(username, password);
if (webClient.OpenRead(url83).IsConnected) // Here, i want to check
{
XmlTextReader reader1 = new XmlTextReader(webClient.OpenRead(url83));
reader1.WhitespaceHandling = WhitespaceHandling.None;
//Do something
}

As described here best way to check internet connectivity might be something like
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead(url83))
{
XmlTextReader reader1 = new XmlTextReader(stream);
reader1.WhitespaceHandling = WhitespaceHandling.None;
//Do something
}
}
catch (WebException ex)
{
// occurs when any error occur while reading from network stream
}

Related

Download multiple files over FTP using one connection in .NET Core

I am trying to download multiple files to memory with only one open connection.
I know from this documentation: https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.keepalive – that FtpWebRequest defaults KeepAlive to true.
if the connection to the server should not be destroyed; otherwise, false. The default value is true.
I have created this piece of code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ConsoleApp23
{
class Program
{
static void Main(string[] args)
{
String[] files = FTPListTree("ftp://server.com", "user", "password");
for (int i = 0; i <= files.Count() - 1; i++)
{
string path = files[i].ToString();
var request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user", "password");
request.Timeout = 100000;
request.KeepAlive = true;
request.ConnectionGroupName = "DownloadFilesGroup";
request.ServicePoint.ConnectionLimit = 8;
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
var ms = new MemoryStream();
stream.CopyTo(ms);
var a = ms.ToArray();
string ss = Encoding.UTF8.GetString(a);
Console.WriteLine(ss);
}
}
}
}
public static String[] FTPListTree(String ftpUri, String user, String pass)
{
try
{
List<String> files = new List<String>();
Queue<String> folders = new Queue<String>();
folders.Enqueue(ftpUri);
while (folders.Count > 0)
{
String fld = folders.Dequeue();
List<String> newFiles = new List<String>();
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
ftp.Timeout = 20000;
ftp.Credentials = new NetworkCredential(user, pass);
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream()))
{
String line = resp.ReadLine();
while (line != null)
{
newFiles.Add(line.Trim());
line = resp.ReadLine();
}
}
ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
ftp.Timeout = 20000;
ftp.Credentials = new NetworkCredential(user, pass);
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream()))
{
String line = resp.ReadLine();
while (line != null)
{
if (!Path.HasExtension(line))
{
String dir = newFiles.First(x => line.EndsWith(x));
newFiles.Remove(dir);
folders.Enqueue(fld + dir + "/");
}
line = resp.ReadLine();
}
}
files.AddRange(from f in newFiles select fld + f);
}
return files.ToArray();
}
catch (Exception e)
{
throw;
}
}
}
}
Is this a proper way of doing dispose? I can't seem to find any documentation on if disposing the response or the response stream, will close the control connection, that the keepalive will keep open.
Is there any way I can log this when running this, so I can see the FTP calls? I don't have access to the FTP server myself.
UPDATE
I have actually tested this with a test FTP and the above code, even though it has KeepAlive=true, it uses new connection on every request. I have created an PoC with Fluent API: https://github.com/robinrodricks/FluentFTP/blob/master/FluentFTP.CSharpExamples/DownloadManyFiles.cs, that does it correctly: connect once, and downloads files. I had a feeling that it was the case with FtpWebRequest. I don't understand the KeepAlive and what it does. Maybe it reuses the TCP connection, which is a good thing in itself, but it uses the TCP connection to login on each request.
Does anybody know if this is even possible with FtpWebRequest, not logging in on each new request, like the Fluent FTP API? Am I missing something?
An example of the FTP log can be seen here
FTP is FileZilla Server 0.9.60 beta.
It seems that the FTP connection reuse was not implemented in .NET Core:
https://github.com/dotnet/runtime/issues/23256
And as *WebRequest API is actually obsolete, it probably never will.
With .NET framework, it works as expected:
FTP multiple files in C# without reestablishing connection

C# The remote server returned an error (401) unauthorized

I am trying to download a xml file from an url but I get this error:
"The remote server returned an error (401) unauthorized". Also, this webpage needs some credentials.
I searched on different topics before coming to ask you, but I didn't find a solution to this...
Here are two versions of codes I tried but didn't work:
try
{
WebClient wClient = new WebClient();
wClient.Credentials = new NetworkCredential(nni, pwd);
var dlString = wClient.DownloadString(url);
//Stream data = wClient.OpenRead(url);
//var reader = new XmlTextReader(wClient.OpenRead(url));
//doc.LoadXml();
doc.Load(wClient.OpenRead(url));
Console.WriteLine(doc.InnerText);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(xmlUrl);
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(nni, pwd);
string htmlCode = client.DownloadString(xmlUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Can you guys please help me on this?

Get confirmation or error after FTP upload

Using the script below, I uploaded a file to a FTP server. It worked, but would be nice if the script would also show a message box with a confirmation if the upload is successful. Or a message box displaying an error code if the upload failed. Any help please?
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://example.com/target.txt", "STOR", localFilePath);
}
I know I should do something like this:
byte[] responseArray = client.UploadFile("ftp://example.com/target.txt", localFilePath);
string s = System.Text.Encoding.ASCII.GetString(responseArray);
I just don't know how to put the pieces toghether.
You could try to use a Try & Catch
https://msdn.microsoft.com/en-us/library/xtd0s8kd(v=vs.110).aspx
Ok, found a good solution to this:
bool exception = false;
try
{
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(FtpUser.Text, FtpPass.Text);
client.UploadFile("ftp://example.com/file.txt", "STOR", MyFilePath);
}
}
catch (Exception ex)
{
exception = true;
MessageBox.Show(ex.Message);
}
if(!exception){
MessageBox.Show("Upload worked!");
}

Facebook integration for login

I am having trouble with integration with Facebook for my site.
Here is the code:
private static string GetFacebookUserJSON(string access_token)
{
string url = string.Format(
"https://graph.facebook.com/me?access_token={0}&fields=email,name,first_name,last_name,link",
access_token);
WebClient wc = new WebClient();
Stream data = wc.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
Unfortunately, it gives me a WebException was unhandled by user code on the OpenRead() line. Additionally, I have the information that I got a (400) Bad Request from the contacted server.
How can I solve this?
Use this to detect the exact error:
private static string GetFacebookUserJSON(string access_token)
{
try
{
string url = string.Format("https://graph.facebook.com/me?access_token={0}&fields=email,name,first_name,last_name,link", access_token);
WebClient wc = new WebClient();
Stream data = wc.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
catch (WebException wex)
{
string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
return pageContent;
}
}
Or I would suggest using Fiddler and updating your question with the response from it's log
Have a further look here for more information about Facebook login securities.

Reading data from an open HTTP stream

I am trying to use the .NET WebRequest/WebResponse classes to access the Twitter streaming API here "http://stream.twitter.com/spritzer.json".
I need to be able to open the connection and read data incrementally from the open connection.
Currently, when I call WebRequest.GetResponse method, it blocks until the entire response is downloaded. I know there is a BeginGetResponse method, but this will just do the same thing on a background thread. I need to get access to the response stream while the download is still happening. This just does not seem possible to me with these classes.
There is a specific comment about this in the Twitter documentation:
"Please note that some HTTP client libraries only return the response body after the connection has been closed by the server. These clients will not work for accessing the Streaming API. You must use an HTTP client that will return response data incrementally. Most robust HTTP client libraries will provide this functionality. The Apache HttpClient will handle this use case, for example."
They point to the Appache HttpClient, but that doesn't help much because I need to use .NET.
Any ideas whether this is possible with WebRequest/WebResponse, or do I have to go for lower level networking classes? Maybe there are other libraries that will allow me to do this?
Thx
Allen
I ended up using a TcpClient, which works fine. Would still be interested to know if this is possible with WebRequest/WebResponse though. Here is my code in case anybody is interested:
using (TcpClient client = new TcpClient())
{
string requestString = "GET /spritzer.json HTTP/1.1\r\n";
requestString += "Authorization: " + token + "\r\n";
requestString += "Host: stream.twitter.com\r\n";
requestString += "Connection: keep-alive\r\n";
requestString += "\r\n";
client.Connect("stream.twitter.com", 80);
using (NetworkStream stream = client.GetStream())
{
// Send the request.
StreamWriter writer = new StreamWriter(stream);
writer.Write(requestString);
writer.Flush();
// Process the response.
StreamReader rdr = new StreamReader(stream);
while (!rdr.EndOfStream)
{
Console.WriteLine(rdr.ReadLine());
}
}
}
BeginGetResponse is the method you need. It allows you to read the response stream incrementally:
class Program
{
static void Main(string[] args)
{
WebRequest request = WebRequest.Create("http://stream.twitter.com/spritzer.json");
request.Credentials = new NetworkCredential("username", "password");
request.BeginGetResponse(ar =>
{
var req = (WebRequest)ar.AsyncState;
// TODO: Add exception handling: EndGetResponse could throw
using (var response = req.EndGetResponse(ar))
using (var reader = new StreamReader(response.GetResponseStream()))
{
// This loop goes as long as twitter is streaming
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
}, request);
// Press Enter to stop program
Console.ReadLine();
}
}
Or if you feel more comfortable with WebClient (I personnally prefer it over WebRequest):
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
client.OpenReadCompleted += (sender, e) =>
{
using (var reader = new StreamReader(e.Result))
{
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
};
client.OpenReadAsync(new Uri("http://stream.twitter.com/spritzer.json"));
}
Console.ReadLine();
Have you tried WebRequest.BeginGetRequestStream() ?
Or something like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (http://www.twitter.com );
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadLine();
while(str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}

Categories

Resources