C # как обрабатывать многочастный файл, загруженный с Android на C # - PullRequest
0 голосов
/ 25 июня 2018

Я загружаю файл Excel с Android на C #, запрос с Android поступает на сервер C # (я не хочу использовать asp.net).

Я с нуля построил простой HTTPServe, я не знаю, как обрабатывать многочастную загрузку данных в c #, это мой код:

Request.cs:

  class Request
{

    public String type { get; set; }
    public String url { get; set; }

    public String host { get; set; }

    private Request(String type,String url,String host)
    {
        this.type = type;

        this.url = url;

        this.host = host;
    }


    public static Request GetRequest(String request)
    {
        if (String.IsNullOrEmpty(request))
            return null;

        String[] tokens = request.Split(' ');      

        String type = tokens[0];

        String url = tokens[1];

        String host = tokens[4];




        return new Request(type, url, host);



    }
}

Response.cs: Ответ класса {

    private Byte[] data = null;

    private String status;

    private String mime;


    private Response(String status,String mime,Byte[] data)
    {
        this.status = status;
        this.data = data;
        this.mime = mime;

    }


    public static Response From(Request request)
    {

        Console.WriteLine(request.type);
        if (request == null)
            return MakeNullRequest();



        if (request.type.Equals("POST"))
        {
            return UploadCompleteResponse(request);

        }

        return MakeFromExcel();

    }




    private static Response UploadCompleteResponse(Request request)
    {   //Handling the multipart request here but I don't know how that is a simple example 
        Console.WriteLine("uploaded!!");
        byte[] bytes = Encoding.ASCII.GetBytes("uploaded!!");

        return new Response("ok 200","text/plain",bytes);


    }



    //private static Response MakeFromFile(FileInfo f)
    //{
    //  // to do later 
    //    //return new Response("200 ok", "text/html", d);
    //}



    //Retruning an excel file response  
    private static Response MakeFromExcel()
    {
        String file = Environment.CurrentDirectory + HTTPServer.EXCEL_DIR+"brains.xlsx";
        Console.WriteLine(file);
        FileInfo info = new FileInfo(file);
        if (info.Exists)
        {
            FileStream fs = info.OpenRead();
            BinaryReader read = new BinaryReader(fs);
            Byte[] d = new Byte[fs.Length];
            read.Read(d, 0, d.Length);
            fs.Close();
            return new Response("200 ok", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", d);
        }

        return PageNotFound();


    }

    private static Response MakeNullRequest()
    {

        String file = Environment.CurrentDirectory+HTTPServer.MSG_DIR +"400.html";
        Console.WriteLine(file);
        FileInfo info = new FileInfo(file);
        FileStream fs=info.OpenRead();
        BinaryReader read=new BinaryReader(fs);
        Byte[] d = new Byte[fs.Length];
        read.Read(d, 0, d.Length);

        fs.Close();
        return new Response("400 Bad Request", "text/html", d);
    }


    private static Response PageNotFound()
    {

        String file = Environment.CurrentDirectory + HTTPServer.MSG_DIR + "400.html";
        FileInfo info = new FileInfo(file);
        FileStream fs = info.OpenRead();
        BinaryReader read = new BinaryReader(fs);
        Byte[] d = new Byte[fs.Length];
        read.Read(d, 0, d.Length);
        return new Response("404 Not Found", "text/html", d);
    }

    public void Post(NetworkStream stream)
    {
        StreamWriter writer = new StreamWriter(stream);

        writer.WriteLine(String.Format("{0} {1}\r\nServer: {2}\r\nContent-Type: {3}\r\nAccept-Ranges: 255 bytes\r\nContent-Length: {4}\r\n", HTTPServer.VERSION
            , status, HTTPServer.NAME, mime, data.Length));
        writer.Flush();
        stream.Write(data, 0, data.Length);
        stream.Flush();
        stream.Close();

    }
}

В приведенном выше коде я использовал более одного типа ответа. Вы можете получить доступ к полям запроса, увеличив размер массива токенов.

HTTServer.cs:

    class HTTPServer
{
    public const String MSG_DIR = "\\root\\msg\\";
    public const String EXCEL_DIR = "\\root\\excel\\";
    public const String WEB_DIR = "\\root\\web\\";
    public const String VERSION = "HTTP/1.1";
    public const String NAME = "Thecode007 HTTP SERVER v 1.0";
    private bool running = false;

    private TcpListener listener;



    public HTTPServer(int port)
    {
        listener = new TcpListener(IPAddress.Any,port);
    }


    public void Start() {

        Thread serverThread = new Thread(new ThreadStart(Run));

        serverThread.Start();

    }


    private void Run()
    {

        running = true;

        listener.Start();

        while (running)
        {

            Console.WriteLine("Waiting for connection...");
            TcpClient client = listener.AcceptTcpClient();

            HandleClient(client);

              Console.WriteLine("Client connected!");





        }

        running = false;

         listener.Stop();
    }

    private void HandleClient(TcpClient client)
    {
        StreamReader reader = new StreamReader(client.GetStream());

        String msg = "";

        while (reader.Peek() != -1)
        {

            msg += reader.ReadLine()+"\n";

        }



        Console.WriteLine(msg);


        Request req = Request.GetRequest(msg);

        Response resp = Response.From(req);


        resp.Post(client.GetStream());
    }



}

А вот основной метод:

class Program
{
    static void Main(string[] args)
    {


        Console.WriteLine("Starting Serving on port 9000");

        HTTPServer server = new HTTPServer(9000);

        server.Start();

        Console.ReadLine();
    }
}

Ответы [ 2 ]

0 голосов
/ 26 июня 2018

Response.cs:

 class Response
   {

    private Byte[] data = null;

    private String status;

    private String mime;


    private Response(String status,String mime,Byte[] data)
    {
        this.status = status;
        this.data = data;
        this.mime = mime;

    }


    public static Response From(Request request)
    {


        if (request == null)
            return MakeNullRequest();



        if (request.type.Equals("POST"))
        {
            return UploadCompleteResponse(request);

        }

        return MakeFromExcel();

    }




    private static Response UploadCompleteResponse(Request request)
    {  //I added on the rquest array the datapart which contains data
       String data=request.data;


        byte[] bytes = Encoding.ASCII.GetBytes("uploaded!!");

        return new Response("ok 200","text/plain",bytes);


    }



    //private static Response MakeFromFile(FileInfo f)
    //{
    //  // to do later 
    //    //return new Response("200 ok", "text/html", d);
    //}



    //Retruning an excel file response  
    private static Response MakeFromExcel()
    {
        String file = Environment.CurrentDirectory + HTTPServer.EXCEL_DIR+"brains.xlsx";
        Console.WriteLine(file);
        FileInfo info = new FileInfo(file);
        if (info.Exists)
        {
            FileStream fs = info.OpenRead();
            BinaryReader read = new BinaryReader(fs);
            Byte[] d = new Byte[fs.Length];
            read.Read(d, 0, d.Length);
            fs.Close();
            return new Response("200 ok", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", d);
        }

        return PageNotFound();


    }

    private static Response MakeNullRequest()
    {

        String file = Environment.CurrentDirectory+HTTPServer.MSG_DIR +"400.html";
        Console.WriteLine(file);
        FileInfo info = new FileInfo(file);
        FileStream fs=info.OpenRead();
        BinaryReader read=new BinaryReader(fs);
        Byte[] d = new Byte[fs.Length];
        read.Read(d, 0, d.Length);

        fs.Close();
        return new Response("400 Bad Request", "text/html", d);
    }


    private static Response PageNotFound()
    {

        String file = Environment.CurrentDirectory + HTTPServer.MSG_DIR + "400.html";
        FileInfo info = new FileInfo(file);
        FileStream fs = info.OpenRead();
        BinaryReader read = new BinaryReader(fs);
        Byte[] d = new Byte[fs.Length];
        read.Read(d, 0, d.Length);
        return new Response("404 Not Found", "text/html", d);
    }

    public void Post(NetworkStream stream)
    {

        try
        {
               StreamWriter writer = new StreamWriter(stream);

        writer.WriteLine(String.Format("{0} {1}\r\nServer: {2}\r\nContent-Type: {3}\r\nAccept-Ranges: 255 bytes\r\nContent-Length: {4}\r\n", HTTPServer.VERSION
            , status, HTTPServer.NAME, mime, data.Length));
        writer.Flush();
        stream.Write(data, 0, data.Length);
        stream.Flush();

        stream.Close();

        }catch(Exception  ex){


        }




    }
}

Запрос.никогда не отправлял ....

0 голосов
/ 25 июня 2018

Отказ от ответственности: Вы никогда не должны писать HTTP-сервер напрямую с TcpClient для любой производственной работы.Слишком много спецификаций для учета, и C # встроил их в любой веб-сервер Asp.Net, который вы используете.

При этом для упражнения необходимо проанализировать запрос в конкретномпуть.Вы должны быть в состоянии обратиться к этим вопросам, чтобы узнать больше о деталях границы раздела:

По сути, вам придется прочитать заголовок из заголовков http (Content-Type) и проанализировать значение boundary= дляопределить, что отделяет пары ключ / значение из составной формы в теле запроса.Как только вы получите границу, вам нужно будет проанализировать тело запроса в виде пар ключ / значение в форме.Оттуда содержимое загруженного файла будет значением любого ключа, который отправляется формой.

Подробнее об официальной спецификации W3

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...