Answer the question
In order to leave comments, you need to log in
How to organize file exchange between PHP server and C# desktop application?
Hello. I am developing my first client-server application, and there is a task to download files from the server and upload them back. I know that the easiest way is WebClient , but my task is more complicated - the files themselves must be stored secretly, and certain logic must be performed on the server side (issue this or that file, or even refuse, etc.).
I see the algorithm like this:
Answer the question
In order to leave comments, you need to log in
Receiving files and storing them are 2 completely unrelated tasks,
Sending a php file
https://habr.com/ru/post/151795/
receiving a file
WebRequest request = WebRequest.Create("http://somesite.com/private/myfile.txt");
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
response.Close();
I'll leave here what I picked up, maybe it will come in handy for someone (so far only for downloading the file):
using System.Net;
using System.IO;
///returns: путь к файлу при успешном скачивании, иначе текст ошибки
public static string DownloadDistr()
{
string folder = "C:\downloads";
//формирую строку запроса
string data = "email=" + userEmail + "&" + "password=" + userPassword;
HttpWebRequest request = HttpWebRequest.CreateHttp("http://yoursite.com/downloadfile.php");
request.Method = "POST";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
//отправляю web запрос
try
{
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
}
catch (Exception ex)
{
return "Error: " + ex.Message;
}
//получаю ответ
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
return response.StatusCode.ToString();
}
//получаю из headers имя файла
WebHeaderCollection headers = response.Headers;
//вот тут как-то тупо, но и так сойдет
string checkFilename = headers["Content-Disposition"].Split('=').Last();
string pathToSave = Path.Combine(folder, checkFilename);
//сохраняю файл через байт-буфер и FileStream
byte[] buffer = new byte[1024];
int read;
using (FileStream download = new FileStream(pathToSave, FileMode.Create))
{
using (Stream stream = response.GetResponseStream())
{
while ((read = stream.Read(buffer, 0, buffer.Length)) != 0)
{
download.Write(buffer, 0, read);
}
}
}
return pathToSave;
}
}
catch (Exception ex)
{
return "Error: " + ex.Message;
}
}
<?php
$userEmail = "unknownid";
$userPassword = "unknownPass";
//получаю данные из запроса
if (isset($_POST['email']) && isset($_POST['password'])) {
$userEmail = strip_tags($_POST['email']);
$userPassword = strip_tags($_POST['password']);
} else {
echo ('Error request');
return;
}
//каким-то образом проверяю данные, могу запретить скачивание
if($userEmail != "correctEmail" || $userPassword != "correctpassword") {
header("HTTP/1.1 403 Forbidden");
return;
}
//путь к отдаваемому файлу
$filepath = "download/file.zip";
if (!file_exists($filepath)) {
header("HTTP/1.1 404 Not Found");
return;
}
try {
//на всякий случай очищаю буфер вывода
if (ob_get_level()) {
ob_end_clean();
}
//header из которого получу имя файла
header('Content-Disposition: attachment; filename=' . basename($filepath));
//а эти headers нужны в первую очередь для браузера, оставил на всякий случай
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));+
//отдаю файл
readfile($filepath);
exit;
} catch (Exception $ex) {
echo $ex->getMessage();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question