我无法使用DoPostMethod发送数组。 Php代码正在运行,但我无法将其转换为C#.Php代码
$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);
我的代码在
下面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();}
但我可以用ajax方法发送
$.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")
}
我认为我的requestData错了。我的RequestData如下:
string xmlRequest = "[{{apiNo=\"apiNo\"}, {user=\"user_name\"},{pass=\"password\"},{mesaj=\"DENEME2\"},{numaralar=\"{0000000000}\"},{baslik=\"baslik\"}}]";
我该怎么做?感谢
答案 0 :(得分:0)
这是一种超级基本的方法。在这个例子中,我用一个返回字符串的快速函数手动写出post参数但是,你可以使用JavascriptSerializer来正确转换C#数组(以及读取你的webservice返回的入站json),这是建议在another post回答。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace doPostExampleForStackOverflow
{
static class Program
{
[STAThread]
static void Main()
{
string postReturn = doPostCall();
MessageBox.Show(postReturn);
Application.Exit();
}
static string doPostCall()
{
string URI = "https://webserver.com/post.php";
string myParameters = "username=" + Environment.UserName; /* Get local username (Windows) */
string result;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
result = wc.UploadString(URI, myParameters);
}
if (result.Length <= 0) {
return "Nothing came back from the webserver.";
} else {
return result;
}
}
}
}
答案 1 :(得分:0)
我用这个
解决了我的问题 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();
您可以查看此link