发送数据后asp.net内容类型问题

时间:2022-02-07 15:37:06

This is my code for send post query to the server:

这是我发送邮件查询到服务器的代码:

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();
    }

And ActionResult:

[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");
    }

But this code worked only when I send data in format: to=zicise@mail.ru&from=narkom@info.by&title=Second method&message=test message and he is broken when I try to cut my custom string like: "to="+value+"&from="+value+"&title="+value+"&message="+value

但是这个代码只有在我以格式发送数据时才有效:to = zicise @ mail.ru&from = narkom @info.by&title =第二种方法&message =测试消息,当我尝试剪切我的自定义字符串时,他就被打破了:“to =”+值+ “&从=” +值+ “&标题=” +值+ “&消息=” +值

But I need to send data from model and get 0 answer. After that PHP script send me email with data. Anyone know, how to convert object in json format name=value&name=value

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

Maybe I need to change Content Type and send jsonString to the server? But if that true, what format I need to use?

也许我需要更改内容类型并将jsonString发送到服务器?但如果这是真的,我需要使用什么格式?

1 个解决方案

#1


0  

You need to URL encode the data.

您需要对数据进行URL编码。

"to="+value+"&from="+value+"&title="+value+"&message="+value

should be changed to:

应改为:

"to="+HttpUtility.UrlEncode (value)+"&from="+HttpUtility.UrlEncode(value)+"&title="+HttpUtility.UrlEncode(value)+"&message="+HttpUtility.UrlEncode (value)

#1


0  

You need to URL encode the data.

您需要对数据进行URL编码。

"to="+value+"&from="+value+"&title="+value+"&message="+value

should be changed to:

应改为:

"to="+HttpUtility.UrlEncode (value)+"&from="+HttpUtility.UrlEncode(value)+"&title="+HttpUtility.UrlEncode(value)+"&message="+HttpUtility.UrlEncode (value)