Autor Beitrag
GerhardS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Mo 28.04.14 01:20 
Ich bin neu in C#, deshalb arbeite ich zunächst mit den vorgegebenen Beispielen - hier für LibCurlNet. Ich möchte damit einen FTP-Transfer realisieren.
Mein Code:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using SeasideResearch.LibCurlNet;

namespace FTPUpload
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read);
                Easy easy = new Easy();
                Easy.ReadFunction rf = new Easy.ReadFunction(OnReadData);
                easy.SetOpt(CURLoption.CURLOPT_READFUNCTION, rf);
                easy.SetOpt(CURLoption.CURLOPT_READDATA, fs);
                Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
                easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
                easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
                Easy.ProgressFunction pf = new Easy.ProgressFunction(OnProgress);
                easy.SetOpt(CURLoption.CURLOPT_PROGRESSFUNCTION, pf);
                easy.SetOpt(CURLoption.CURLOPT_URL, args[1]);
                easy.SetOpt(CURLoption.CURLOPT_USERPWD,
                    args[2] + ":" + args[3]);
                easy.SetOpt(CURLoption.CURLOPT_UPLOAD, true);
                easy.SetOpt(CURLoption.CURLOPT_INFILESIZE, fs.Length);
                easy.Perform();
                easy.Cleanup();
                fs.Close();
                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        } //Main
        public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb,
        Object extraData)
        {
            FileStream fs = (FileStream)extraData;
            return fs.Read(buf, 0, size * nmemb);
        }


        public static void OnDebug(CURLINFOTYPE infoType, String msg,
            Object extraData)
        {
            Console.WriteLine(msg);
        }


        public static Int32 OnProgress(Object extraData, Double dlTotal,
            Double dlNow, Double ulTotal, Double ulNow)
        {
            Console.WriteLine("Progress: {0} {1} {2} {3}",
                dlTotal, dlNow, ulTotal, ulNow);
            return 0// standard return from PROGRESSFUNCTION
        }
    } //Program
//Namespace

Der Code wird problemlos kompiliert, die Konsolen-Ausführung bringt bei diesem Befehl:
FTPUpload testdatei.txt www.wunschname.de/testdatei.txt username Password
die Antwort: Error 405, Method not allowed
während bei diesem Befehl
FTPUpload testdatei.txt www.wunschname.de username Password
gemeldet wird:
ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
About to connect() to www.wunschname.de port 80

  Trying 81.169.145.xxx... 
connected

Connected to www.wunschname.de (81.169.145.xxx) port 80

Server auth using Basic with user 'ich'

PUT / HTTP/1.1
Authorization: Basic c2V5c28uZGU6MjAwNg==
Host: www.wunschname.de
Pragma: no-cache
Accept: */*
Content-Length: 33
Expect: 100-continue


HTTP/1.1 100 Continue

Progress: 0 0 33 0
Das ist eine Testdatei.
Ende.+++
HTTP/1.1 302 Moved Temporarily

Date: Sun, 27 Apr 2014 23:05:19 GMT

Server: Apache/2.2.26 (Unix)

X-Powered-By: PHP/5.2.17

location: http://www.wunschname.de/willkommen_de.php

Content-Length: 0

Content-Type: text/html; charset=UTF-8

Progress: 0 0 33 33
Connection #0 to host www.wunschname.de left intact
.
In beiden Fällen wird die Datei testdatei.txt nicht übertragen.
Was läuft da schief?

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Mo 28.04.2014 um 08:30