C# can';无法从post请求应答中获取0

C# can';无法从post请求应答中获取0,c#,asp.net,json,post,request,C#,Asp.net,Json,Post,Request,我需要发送电子邮件感谢php脚本。在服务器中,我向服务器发送了请求,并得到了答案。如果一切都好,我得到0,如果我得到1,一切都不好 这是我的代码: public class MessagePost { public string to { get; set; } public string from { get; set; } public string title { get; set; } public string message { get; set; }

我需要发送电子邮件感谢php脚本。在服务器中,我向服务器发送了请求,并得到了答案。如果一切都好,我得到0,如果我得到1,一切都不好

这是我的代码:

public class MessagePost
{
    public string to { get; set; }
    public string from { get; set; }
    public string title { get; set; }
    public string message { get; set; }
}

[HttpPost]
    public ActionResult SendMessage(FEEDBACK feedbackModel)
    {
        try
        {
            string FromEmail = feedbackModel.sEmail;
            string toEmail = "zicise@mail.ru";
            string title = "Сообщение с сайта народный комунальщик от " + feedbackModel.vFIO;
            string message = feedbackModel.vMessage;

            MessagePost MP = new MessagePost();

            MP.to = toEmail;
            MP.from = FromEmail;
            MP.title = title;
            MP.message = message;

            List<MessagePost> dataMessage = new List<MessagePost>();

            dataMessage.Add(MP);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(dataMessage);

            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://projects.pushnovn.com/send_email/");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = json;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

            string convertResult = System.Text.Encoding.UTF8.GetString(byteArray);

            RootObject r = JsonConvert.DeserializeObject<RootObject>(responseFromServer);

            if (r.code == 0)
            {
                ViewBag.RedirectMessage = r.msg;
                return View("~/Views/Home/RedirectPage.cshtml");
            }
            else
            {
                ViewBag.RedirectMessage = r.msg;
                return View("~/Views/Home/RedirectPage.cshtml");
            }
        }
        catch (Exception exc)
        {
            ViewBag.RedirectMessage = "Невозможно отправить e-mail - error: " + exc.Message;
            return View("~/Views/Home/RedirectPage.cshtml");
        }
    }
公共类MessagePost
{
{get;set;}的公共字符串
来自{get;set;}的公共字符串
公共字符串标题{get;set;}
公共字符串消息{get;set;}
}
[HttpPost]
公共行动结果发送消息(反馈模型)
{
尝试
{
字符串FromEmail=feedbackModel.sEmail;
字符串toEmail=”zicise@mail.ru";
字符串title=“Сббббаааааааааааааааа;
字符串消息=feedbackModel.vMessage;
MessagePost MP=newmessagepost();
MP.to=电子邮件;
MP.from=FromEmail;
MP.title=标题;
MP.message=消息;
List dataMessage=新列表();
dataMessage.Add(MP);
JavaScriptSerializer serializer=新的JavaScriptSerializer();
string json=serializer.Serialize(dataMessage);
//使用可以接收帖子的URL创建请求。
WebRequest=WebRequest.Create(“http://projects.pushnovn.com/send_email/");
//将请求的Method属性设置为POST。
request.Method=“POST”;
//创建POST数据并将其转换为字节数组。
字符串postData=json;
byte[]byteArray=Encoding.UTF8.GetBytes(postData);
//设置WebRequest的ContentType属性。
request.ContentType=“application/x-www-form-urlencoded”;
//设置WebRequest的ContentLength属性。
request.ContentLength=byteArray.Length;
//获取请求流。
Stream dataStream=request.GetRequestStream();
//将数据写入请求流。
写入(byteArray,0,byteArray.Length);
//关闭流对象。
dataStream.Close();
//得到回应。
WebResponse=request.GetResponse();
//显示状态。
Console.WriteLine(((HttpWebResponse)response.StatusDescription);
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();
string convertResult=System.Text.Encoding.UTF8.GetString(byteArray);
RootObject r=JsonConvert.DeserializeObject(responseFromServer);
如果(r.code==0)
{
ViewBag.RedirectMessage=r.msg;
返回视图(“~/Views/Home/RedirectPage.cshtml”);
}
其他的
{
ViewBag.RedirectMessage=r.msg;
返回视图(“~/Views/Home/RedirectPage.cshtml”);
}
}
捕获(异常exc)
{
ViewBag.RedirectMessage=“ззжжааааааа电子邮件-错误:“+exc.消息;
返回视图(“~/Views/Home/RedirectPage.cshtml”);
}
}
但我总是得到1,怎么了

例如数据

致:zicise@mail.ru

发件人:test@mail.ru

题目:考试题目


消息:测试消息

好的。所以,我做了。但不是我想要的。这是我向服务器发送post查询的代码:

public static HttpWebResponse PostMethod(string postedData, string postUrl)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
        request.Method = "POST";
        request.Credentials = CredentialCache.DefaultCredentials;

        UTF8Encoding encoding = new UTF8Encoding();
        var bytes = encoding.GetBytes(postedData);

        //request.ContentType = "application/javascript";
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentType = "application/json; charset=utf-8";
        //request.ContentType = "application/json";
        //request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

        request.ContentLength = bytes.Length;

        using (var newStream = request.GetRequestStream())
        {
            newStream.Write(bytes, 0, bytes.Length);
            newStream.Close();
        }
        return (HttpWebResponse)request.GetResponse();
    }
行动和结果:

[HttpPost]
    public ActionResult SendMessage(FEEDBACK feedbackModel)
    {
        MessageData msgData = new MessageData();

        msgData.to = "zicise@mail.ru";
        msgData.from = feedbackModel.sEmail;
        msgData.title = "Сообщение с сайта наркома от пользователя: " + feedbackModel.vFIO;
        msgData.message = feedbackModel.vFIO;

        var jsonString = JsonConvert.SerializeObject(msgData);

        var response = PostMethod("to=zicise@mail.ru&from=narkom@info.by&title=Second method&message=test message", "http://projects.pushnovn.com/send_email/");
        //var response = PostMethod("to:zicise@mail.ru,from:narkom@info.by,title:Second method,message:test message", "http://projects.pushnovn.com/send_email/");
        //var response = PostMethod("{to:zicise@mail.ru,from:narkom@info.by,title:Second method,message:test message}", "http://projects.pushnovn.com/send_email/");
        //var response = PostMethod("[{to:zicise@mail.ru,from:narkom@info.by,title:Second method,message:test message}]", "http://projects.pushnovn.com/send_email/");
        //var response = PostMethod(jsonString, "http://projects.pushnovn.com/send_email/");

        if (response != null)
        {
            var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            var responseToString = strreader.ReadToEnd();

            RootObject r = JsonConvert.DeserializeObject<RootObject>(responseToString);

            ViewBag.RedirectMessage = r.msg;
        }

        return View("~/Views/Home/RedirectPage.cshtml");
    }
[HttpPost]
公共行动结果发送消息(反馈模型)
{
MessageData msgData=newmessagedata();
msgData.to=”zicise@mail.ru";
msgData.from=feedbackModel.sEmail;
msgData.title=“Саааааааааааааааааааа107;
msgData.message=feedbackModel.vFIO;
var jsonString=JsonConvert.SerializeObject(msgData);
var响应=方法后(“至=zicise@mail.ru&从=narkom@info.by&标题=第二种方法&消息=测试消息“,”http://projects.pushnovn.com/send_email/");
//var响应=方法后(“至:zicise@mail.ru,发件人:narkom@info.by,标题:第二种方法,消息:测试消息“http://projects.pushnovn.com/send_email/");
//var response=PostMethod(“{to:zicise@mail.ru,发件人:narkom@info.by,标题:第二个方法,消息:test message}“http://projects.pushnovn.com/send_email/");
//var response=PostMethod(“[{to:zicise@mail.ru,发件人:narkom@info.by,标题:第二个方法,消息:test message}],“http://projects.pushnovn.com/send_email/");
//var response=PostMethod(jsonString,“http://projects.pushnovn.com/send_email/");
if(响应!=null)
{
var strreader=newstreamreader(response.GetResponseStream(),Encoding.UTF8);
var responseToString=strreader.ReadToEnd();
RootObject r=JsonConvert.DeserializeObject(responseToString);
ViewBag.RedirectMessage=r.msg;
}
返回视图(“~/Views/Home/RedirectPage.cshtml”);
}
但这段代码只有在我以以下格式发送数据时才起作用:
=zicise@mail.ru&从=narkom@info.by&标题=第二种方法&消息=测试消息


但我需要从模型中发送数据并得到0的答案。之后,PHP脚本向我发送包含数据的电子邮件。有人知道,如何将对象转换为json格式
name=value&name=value

,因此您从该代码访问的服务器获得了一个不成功的响应吗?也许答案会出现在回复信息中?或者在服务器的日志中?我不太清楚