Ich habe ja 2 unterschiedliche Methoden, Daten an eine PHP Datei zu senden:
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:
| HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://meineURL.com/Script.php"); myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"; myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
NameValueCollection nv = HttpUtility.ParseQueryString(String.Empty); nv["value"] = this.textBox1.Text; byte[] data = Encoding.ASCII.GetBytes(nv.ToString()); myHttpWebRequest.ContentLength = data.Length;
Stream requestStream = myHttpWebRequest.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream responseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
myStreamReader.Close(); responseStream.Close(); myHttpWebResponse.Close();
MessageBox.Show(pageContent, "Page Content"); |
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
| string URL = @"https://meineURL/Script.php"; WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection(); formData["value"] = this.textBox1.Text;
byte[] responseBytes = webClient.UploadValues(URL, "POST", formData); string responsefromserver = Encoding.UTF8.GetString(responseBytes); webClient.Dispose(); MessageBox.Show(responsefromserver); |
Der Grund, warum ich den Thread eröffnet habe war, dass bei der zweiten Methode der Fehler kam, dass die Verbindung aufgrund einer Unsicherheit geschlossen wurde. Ich habe aus Spaß jetzt ein kleines Programm geschrieben, was diese beiden Methoden enthält. Mit Button1 wird die obere Methode ausgeführt und mit Button2 die untere. Beide funktionieren jetzt. Aber warum? Ich meine es gab ja anfangs Probleme?
Hier sind die Bilder von Charles, der die Datenpakete abfängt:
"Wer keinen Sinn im Leben sieht, ist nicht nur unglücklich, sondern kaum lebensfähig" - Albert Einstein