C# Curl Rückgabe auslesen

Bububoomt

ohne Vertrauen
ID: 10361
L
28 April 2006
19.666
769
Ich habe folgende PHP Class, die ich gern in C# hätte

Code:
class Adns {


    function call ($data) {
        $xml = new XML_Converter();
        $data=to_utf8($data);
        $req = $xml->hash_to_xml( $data );
        
        $res = $this->requestCurl( $req );
        return $xml->xml_to_hash( $res );

    }

    function requestCurl( $request ) {

        $ch = curl_init( HOST );

        curl_setopt( $ch, CURLOPT_POSTFIELDS, $request );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE);



        if( !$response=curl_exec( $ch )) {
        
            echo " Curl execution error". curl_error( $ch ) ."\n".curl_errno($ch)."\n";
            return FALSE;
        }


        curl_close( $ch );

        # remove keep alive header
        while(substr($response, 0, 21) == "HTTP/1.1 100 Continue") {
            $response = substr($response, 25);
        }

        return $response;
    }
}

Ich habe folgendes verwendet:
https://sourceforge.net/projects/libcurl-net/
https://blog.iarivas.com.ar/using-curl-with-c-net/

Mein Code:

Code:
 protected void Page_Load(object sender, EventArgs e)
        { 
            
            sourceContent = new StringBuilder();
            try
            {

                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                Easy easy = new Easy();
                Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
                easy.SetOpt(CURLoption.CURLOPT_URL, @"DIEURL");

                StringWriter strw = new StringWriter();
                XmlTextWriter wr = new XmlTextWriter(strw);
                wr.WriteStartDocument();
                wr.WriteStartElement("request");

                //-->Authblock
                wr.WriteStartElement("auth");
                wr.WriteElementString("user", "xxxx");
                wr.WriteElementString("password", "xxxxxx");
                wr.WriteElementString("context", "xxxx");
                wr.WriteEndElement();
                //<--Authblock

        //Mehr XMLDaten

                wr.WriteEndElement();
                //<--task
                wr.WriteEndElement();
                wr.WriteEndDocument();
                easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, strw.ToString());
                easy.SetOpt(CURLoption.CURLOPT_POST, 1);
                easy.SetOpt(CURLoption.CURLOPT_READDATA, true);
                easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, true);
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
                easy.Perform();
                easy.Cleanup();
                Curl.GlobalCleanup();

            }
            catch (Exception ex)
            {
    
            }

            ausgabe.InnerHtml = sourceContent.ToString();
        }

        public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            this.sourceContent.Append(System.Text.Encoding.UTF8.GetString(buf));
            return size * nmemb;
        }

Leider erhalte ich keine Rückgabe auch lande ich gar nicht in der OnWriteData beim Debuggen.

Jemand eine Idee??
 
Jetzt habe ich mal folgendes versucht:

Code:
 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://URL/");
                //Defined poperties for the Web-Request
                httpWebRequest.Method = "POST";
                httpWebRequest.MediaType = "HTTP/1.1";
                httpWebRequest.ContentType = "text/xml";
                httpWebRequest.UserAgent = "Example Client";

                //Defined data for the Web-Request
                byte[] byteArrayData = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><request><auth><user>xxxx</user><password>xxxx</password><context>XXX</context></auth></request>");
                httpWebRequest.ContentLength = byteArrayData.Length;

                //Attach data to the Web-Request
                Stream dataStream = httpWebRequest.GetRequestStream();
                dataStream.Write(byteArrayData, 0, byteArrayData.Length);
                dataStream.Close();

                //Send Web-Request and receive a Web-Response
                HttpWebResponse httpWebesponse = (HttpWebResponse)httpWebRequest.GetResponse();

                //Translate data from the Web-Response to a string
                dataStream = httpWebesponse.GetResponseStream();
                StreamReader streamreader = new StreamReader(dataStream, Encoding.UTF8);
                string response = streamreader.ReadToEnd();
                streamreader.Close();
Damit bekomme ich nun auch eine Rückgabe leider aber folgendes:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<response>
  <result>
    <msg>
      <code>EF00000</code>
      <help></help>
      <object>
        <type>XML error</type>
        <value>xmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: key line 1 and unparseablexmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: task line 1 and unparseablexmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: request line 1 and unparseableExtra content at the end of the document at /usr/lib/perl5/XML/LibXML/SAX.pm line 64 at /usr/share/perl5/XML/Simple.pm line 295</value>
      </object>
      <text>Fehler beim Lesen des Auftrages.</text>
      <type>error</type>
    </msg>
    <status>
      <code>E00000</code>
      <text>Es sind Fehler bei der Verarbeitung aufgetreten.</text>
      <type>error</type>
    </status>
  </result>
</response>
<?xml version="1.0" encoding="utf-8" ?>
<response>
  <result>
    <msg>
      <code>EF00000</code>
      <help></help>
      <object>
        <type>XML error</type>
        <value>xmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: key line 1 and unparseablexmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: task line 1 and unparseablexmlParseStartTag: invalid element nameexpected '>'Opening and ending tag mismatch: request line 1 and unparseableExtra content at the end of the document at /usr/lib/perl5/XML/LibXML/SAX.pm line 64 at /usr/share/perl5/XML/Simple.pm line 295</value>
      </object>
      <text>Fehler beim Lesen des Auftrages.</text>
      <type>error</type>
    </msg>
    <status>
      <code>E00000</code>
      <text>Es sind Fehler bei der Verarbeitung aufgetreten.</text>
      <type>error</type>
    </status>
  </result>
</response>
jemand eine Idee? :(