Bilgesu Erdoğan Bilgesu Erdoğan - 15 days ago 5
C# Question

How to send array HttpPost to Apache Server

I can't sending my array with DoPostMethod. Php code is working but I can't convert it to C#.Php code is

$numbers = array('50XXXXXXXX', '50XXXXXXXX', '50XXXXXXXX', '50XXXXXXXX');
$message = 'TEST';
$title = 'MAS API';
$veriler = array(
'apiNo' =>'1',
'user' =>’user_name’,
'pass' =>'pasword',
'mesaj'=>$message,
'numaralar' =>$numbers,
'baslik' =>$title,
);
$ozel_mesaj = sms_gonder("http://------",$veriler);


My code is below

DoRequest(string requestUrl, string requestMethod, string requestData){
WebRequest request = WebRequest.Create(requestUrl);

request.Method = requestMethod;


string postData = requestData;
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);


this.ServerResponse = reader.ReadToEnd();


reader.Close();
dataStream.Close();
response.Close();}


but i can send with ajax method

$.ajax({
"url": "myUrl",
"type": "post",
data:{
"apiNo": "apiNo",
"user": "user_name",
"pass": "password",
"mesaj": "DENEME123",
"numaralar":"numbers",
"baslik":"baslik"
},
success: function (data) {
if (data != NULL)
alert(data)
else
alert("Kayıt Eklenemedi")
}


I think my requestData is wrong. My RequestData is below:

string xmlRequest = "[{{apiNo=\"apiNo\"}, {user=\"user_name\"},{pass=\"password\"},{mesaj=\"DENEME2\"},{numaralar=\"{0000000000}\"},{baslik=\"baslik\"}}]";


How can I do that? Thanks

Answer

I solved my problem using this

     string URL = "myURL";
     WebClient webClient = new WebClient();


     NameValueCollection formData = new NameValueCollection();
    formData["apiNo"] = "apiNo";
        formData["user"] = "user";
        formData["pass"] = "pass";
        formData["baslik"] = "baslik";
        formData["numaralar"] = "{numaralar}";
        formData["mesaj"] =" mesaj";

     byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
     string responsefromserver = Encoding.UTF8.GetString(responseBytes);
     Console.WriteLine(responsefromserver);
     webClient.Dispose();

and you can look at this link

Comments