您的位置:首页 >  接口中心 > 短信接口DEMO-C# Post.cs
  短信接口DEMO-C# Post.cs
 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public partial class Post : System.Web.UI.Page
{
    public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButSubmit_Click(object sender, EventArgs e)
    {
        string un = this.Txtaccount.Text.Trim();
        string pw = this.Txtpassword.Text.Trim();
        string phone = this.Txtmobile.Text.Trim();
        string content = "【云通讯】" + HttpContext.Current.Server.UrlEncode(this.Txtcontent.Text.Trim());


        string postJsonTpl = ""account":"{0}","password":"{1}","phone":"{2}","report":"false","msg":"{3}"";
        string jsonBody = string.Format(postJsonTpl, un, pw, phone, content);
        string result = doPostMethodToObj("http://xxx/msg/send/json""{" + jsonBody + "}");//请求地址请登录云通讯平台查看或者询问您的商务负责人获取
        LabelRetMsg.Text = result;

    }

    public static string doPostMethodToObj(string url, string jsonBody)
    {
        string result = String.Empty;
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        // Create NetworkCredential Object 
        NetworkCredential admin_auth = new NetworkCredential("username""password");

        // Set your HTTP credentials in your request header
        httpWebRequest.Credentials = admin_auth;

        // callback for handling server certificates
        ServicePointManager.ServerCertificateValidationCallback = delegate return true; };

        using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(jsonBody);
            streamWriter.Flush();
            streamWriter.Close();
            HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }
        return result;
    }

}