C# | WebPermission-Exception on XP-machine - c#

I'm writing a small C#-client which checks a homepage on a local server every 5 seconds. On a Windows-7-VM it works, but if I try running the client on a XP-virtual-machine I'm getting a System.Net.WebPermission-Exception. Both VMs have the same network settings (WinXP-IP=10.0.1.100, Win7-IP=10.0.1.101) and their network-adapters are set to bridged networking (in the Win7-VM the client works)
My code:
private string GetPageContent(string url)
{
WebClient client = new WebClient();
string output = client.DownloadString(url);
client.Dispose();
return output;
}
The URL the WebClient tries to download looks like
http://10.0.1.25/info.php
Is this a Windows XP specific problem or do I have to add some permissions?

Related

Get Linux server time from windows service c#

Is there anyway I can get linux server time from windows service given linux server IP ? I tried to use Cliwrap (https://github.com/Tyrrrz/CliWrap) and wrote the following function but it is not working as well:
public string GetLinuxServerTime()
{
using (var cli = new Cli("bash.exe"))
{
// Execute
var output = cli.Execute("ssh user#10.104.12.114 date");
return "abc";
}
}
Kindly suggest some another way.

How to upload a file in share point server from a local machine connected through lan using c#

I am really new to this share point stuffs.We have a share point server with admin account in it and i was connecting it from my local machine through ip and share point port manually.
Nut i need to write a program which needs to upload the files into the share point server from the local machine to server. Is it possible in using winforms ? or only possible in web services.?
using (SPSite oSite = new SPSite(sharePointSite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
if (!System.IO.File.Exists(fileToUpload))
throw new FileNotFoundException("File not found.", fileToUpload);
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
// Prepare to upload
Boolean replaceExistingFiles = true;
String fileName = System.IO.Path.GetFileName(fileToUpload);
FileStream fileStream = File.OpenRead(fileToUpload);
// Upload document
SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
// Commit
myLibrary.Update();
}
}
tried using the above code and i getting error from the following line
using (SPSite oSite = new SPSite(sharePointSite))
and the error was
"The Web application at http://server:port/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application"
and am not able to upload the file.
But if i copied and paste the same URL in my local machine i can able to access the sharepoint deployed in server and i can even upload files manually from my local machine.
How to upload a file in sharepoint server from the local machine connected with LAN..??
siteURL = Main URl of the sharepoint (eg) "http://10.0.0.14:48487/";
documentListName = any of the folders in shrepoint (eg) Shared Documents
documentName = name of the file (eg)sampleword.docx , readme.txt etc
documentStream = the byte format of the file which we going to upload.
(eg)byte[] bytefile = System.IO.File.ReadAllBytes(filepath+filename);
public static void UploadDocument(string siteURL, string documentListName, string documentListURL,string documentName, byte[] documentStream = null)
{
try
{
using (SP.ClientContext clientContext = new SP.ClientContext(siteURL))
{
#region"Only if you have credentials"
NetworkCredential Cred = new NetworkCredential("username", "password");
clientContext.Credentials = Cred;
#endregion
SP.List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new SP.FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = documentStream;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}
catch (Exception ex)
{
}
}
}
This works perfect for me :) :)
make sure your connecting to an existing site collection. the error you have received is quite self explainitory, it cannot find the site collection you have pointed it at. check your string sharePointSite to make sure there isn't a typo and that it's accessing the correct root site collection. Remember with SharePoint SPSite = Site Collection SPWeb = a website in a site collection.
i can't see any glaring errors in your code without running something up myself except to make sure that when you are calling oSite.openweb() it's for a single site within the site collection.

Windows 8 app how to connect to Socket server

i have done a server using this example socketAsyncEventArgs
in visual studio 2010 and .net 4.0.
Now i'm trying to connect to it from a windows 8 app using StreamSocket but i'm getting a "Acces denied" message.
here is the Client code:
private StreamSocket streamSocket;
public string Server = "192.168.0.101";
public int Port = 9900;
public async void Connect()
{
streamSocket = new StreamSocket();
Connect();
try
{
await streamSocket.ConnectAsync(
new Windows.Networking.HostName(Server),
Port.ToString()); // getting Acces Denied here
DataReader reader = new DataReader(streamSocket.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
while (true)
{
var bytesAvailable = await reader.LoadAsync(1000);
var byteArray = new byte[bytesAvailable];
reader.ReadBytes(byteArray);
}
}
catch (Exception e)
{
MessageBox(e.StackTrace);
}
}
How to fix the problem? Is there another way to send and receive messages using this server?
You are probably also seeing the following as part of your error message:
WinRT information: A network capability is required to access this network resource
This is because you need to add a capability to your application that allows you to access local networks. Double click on the Package.appxmanifest file in your project. Click on the Capabilities tab. Add the Private Networks (Client & Server) capability to your project.

Loading data from a site in Silverlight

I'm trying to load data into my Silverlight app. However, when it launches, I get a TargetInvocationException as soon as I hit e.Result:
public MainPage() {
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri("http://www.google.com"));
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
Stream st = e.Result;
StreamReader sr = new StreamReader(st);
String result = sr.ReadToEnd();
}
Why does this fail, and what should I do to make it work?
PS, I'm afraid I cannot make a local proxy, because the app is going to be deployed as part of an app on an Apache Tomcat server, not an IIS.
Cheers
Nik
Silverlight cannot make cross-domain requests without a cross-domain policy file on the target domain. If you can't set up a proxy, you won't be able to get data from any domain other than the one hosting your application.

Uploading files to file server using webclient class

Currently I have an application that receives an uploaded file from my web application. I now need to transfer that file to a file server which happens to be located on the same network (however this might not always be the case).
I was attempting to use the webclient class in C# .NET.
string filePath = "C:\\test\\564.flv";
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential(uName, password);
Uri addy = new Uri("\\\\192.168.1.28\\Files\\test.flv");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, filePath);
Console.WriteLine(arrReturn.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The machine located at 192.168.1.28 is a file server and has a share c:\Files.
As of right now I am receiving an error of Login failed bad user name or password, but I can open explorer and type in that path login successfully. I can also login using remote desktop, so I know the user account works.
Any ideas on this error?
Is it possible to transfer a file directly like that? With the webclient class or maybe some other class?
Just use
File.Copy(filepath, "\\\\192.168.1.28\\Files");
A windows fileshare exposed via a UNC path is treated as part of the file system, and has nothing to do with the web.
The credentials used will be that of the ASP.NET worker process, or any impersonation you've enabled. If you can tweak those to get it right, this can be done.
You may run into problems because you are using the IP address instead of the server name (windows trust settings prevent leaving the domain - by using IP you are hiding any domain details). If at all possible, use the server name!
If this is not on the same windows domain, and you are trying to use a different domain account, you will need to specify the username as "[domain_or_machine]\[username]"
If you need to specify explicit credentials, you'll need to look into coding an impersonation solution.
namespace FileUpload
{
public partial class Form1 : Form
{
string fileName = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "";
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Attach customer proposal document";
fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
fileName = System.IO.Path.GetFileName(fDialog.FileName);
path = Path.GetDirectoryName(fDialog.FileName);
textBox1.Text = path + "\\" + fileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("erandika1986", "123");
Uri addy = new Uri(#"\\192.168.2.4\UploadDocs\"+fileName);
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, textBox1.Text);
MessageBox.Show(arrReturn.ToString());
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
}
}
when you manually open the IP address (via the RUN command or mapping a network drive), your PC will send your credentials over the pipe and the file server will receive authorization from the DC.
When ASP.Net tries, then it is going to try to use the IIS worker user (unless impersonation is turned on which will list a few other issues). Traditionally, the IIS worker user does not have authorization to work across servers (or even in other folders on the web server).

Categories