Hallo,
ich möchte gern ein kleines Programm haben, das über eine URL einen Text an einen SMS Server
sendet, der diesen dann an ein Handy weiter leitet.
Der Aufruf muss wie folgt aussehen:
http://10.130.143.60/api.php?text=%&<tel_nummer>&username=<value>&password=<value>@sms_inhalt
Ich habe nun ein bisschen gegooglet und bin auf verschiedene Arten gestoßen um das zu erledigen.
Leider funktionieren die nicht.
Was genau mach ich falsch?
private void test1(string tel, string sms)
{
WebRequest request = WebRequest.Create("http://10.130.143.60/api.php?text=%&" + tel + "&username=<value>&password=<value>@" + sms); //PW UND USERNAME ERSETZT
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
Variante2:
private void test2(string tel, string sms)
{
string data = "username=<value>&password=<value>@" + sms;
byte[] dataStream = Encoding.UTF8.GetBytes(data);
string urlPath = "http://10.130.143.60/";
string request = urlPath + "api.php?text=%&" + tel;
WebRequest webRequest = WebRequest.Create(request);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
// Send the data.
newStream.Write(dataStream,0,dataStream.Length);
newStream.Close();
WebResponse webResponse = webRequest.GetResponse();
}
Variante 3:
private void test3(string tel, string sms)
{
string data = "?text=%&" + tel + "&username=<value>&password=<value>@" + sms;
MessageBox.Show(data);
byte[] dataStream = Encoding.UTF8.GetBytes(data);
string urlPath = "http://10.130.143.60/";
string request = urlPath + "api.php";
MessageBox.Show(request);
WebRequest webRequest = WebRequest.Create(request);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
// Send the data.
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
WebResponse webResponse = webRequest.GetResponse();
}
thx für die Hilfe
Kaladial
hat sich erledigt
variante 3 hat funktioniert man musste nur
System.Net.ServicePointManager.Expect100Continue = false;
hinzufügen und die syntax für den befehl richtig schreiben
string data = "text=" + sms + "&to=" + tel + "&username=" + user + "&password=" + passwort + "@sms";
mfg Kaladial
Für die Zukunft bitte mehr mitteilen als "funktioniert nicht", denn auch wir können nicht hellsehen - noch nicht.
Siehe auch [Hinweis] Wie poste ich richtig? Punkt 5 😉
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code