c#_1:后台post请求

1:aspx内容

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Push.aspx.cs" Inherits="_jichu_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        #submit_Push
        {
        }
    </style>
</head>
<body>
    <form id="form_Push" runat="server">
        <div style="margin: 0  auto; width: 800px">
            <table>
                <tr>
                    <td>车款ID:</td>
                    <td>
                        <input type="text" id="text_CarModelID" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>VIN码:</td>
                    <td>
                        <input type="text" id="text_Vin" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>发动机号:</td>
                    <td>
                        <input type="text" id="text_EngineNumber" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>车牌号:</td>
                    <td>
                        <input type="text" id="text_CarBrandNumber" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>车辆归属地:</td>
                    <td>
                        <select id="select_CarProvince" runat="server">
                            <option>北京市</option>
                            <option>河南省</option>
                        </select>
                        <select id="select_CarCity" runat="server">
                            <option>北京市</option>
                            <option>郑州市</option>

                        </select>
                        <select id="select_CarDistrict" runat="server">
                            <option>海淀区</option>
                            <option>中原区</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>车辆购置价:</td>
                    <td>
                        <input type="text" id="text_PurchasePrice" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>初登日期:</td>
                    <td>
                        <input type="text" id="text_RegisterDate" runat="server" readonly="true" value="2012-6-1" />
                    </td>
                </tr>
                <tr>
                    <td>发票日期:</td>
                    <td>
                        <input type="text" id="text_InvoiceDate" runat="server" value="2012-8-1" />
                    </td>
                </tr>
                <tr>
                    <td>客户姓名:</td>
                    <td>
                        <input type="text" id="text_ClientName" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>客户手机:</td>
                    <td>
                        <input type="text" id="text_ClientPhone" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>证件类型:</td>
                    <td>
                        <input type="text" id="text_PapersType" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>证件号码:</td>
                    <td>
                        <input type="text" id="text_PapersNumber" runat="server" readonly="true" />
                    </td>
                </tr>
                <tr>
                    <td>客户所在地:</td>
                    <td>
                        <select id="select_ClientProvince" runat="server">
                            <option>北京市</option>
                            <option>河南省</option>
                        </select>
                        <select id="select_ClientCity" runat="server">
                            <option>北京市</option>
                            <option>郑州市</option>
                        </select>
                        <select id="select_ClientDistrict" runat="server">
                            <option>海淀区</option>
                            <option>中原区</option>
                        </select>
                        详细地址:<input type="text" runat="server" id="text_ClientAddress" />
                    </td>
                </tr>
                <tr>
                    <td>客户邮编:</td>
                    <td>
                        <input type="text" id="text_ClientPost" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>客户邮箱:</td>
                    <td>
                        <input type="text" id="text_ClientEmail" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>新旧程度:</td>
                    <td>
                        <select id="select_CarDegree" runat="server">
                            <option>二手车</option>
                            <option>新车</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>签约时间:</td>
                    <td>
                        <input type="text" id="text_SingDate" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>生效时间:</td>
                    <td>
                        <input type="text" id="text_TakeEffectDate" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>起始公里数:</td>
                    <td>
                        <input type="text" id="text_StartKM" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>产品名称:</td>
                    <td>
                        <select id="select_ProdutName" runat="server">
                            <option>专享A套餐</option>
                            <option>268V-B套餐</option>
                        </select>
                    </td>
                </tr>
                <%--                <tr>
                    <td>产品类别:</td>
                    <td>
                        <select id="select_ProdutType" runat="server">
                            <option>动力智选-Day1</option>
                        </select>
                    </td>
                </tr>--%>
                <tr>
                    <td>销售公司:</td>
                    <td>
                        <input type="text" id="text_SaleCompany" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>附件:</td>
                    <td>
                        <span id="span_Enclosure" runat="server">附件1|附件2</span>
                    </td>
                </tr>
                <tr>
                    <td>合同备注:</td>
                    <td>
                        <textarea id="text_ContractRemarks" runat="server"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="submit_Push" runat="server" OnClick="BtnSubmit" Text="推送" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

2:aspx.cs内容


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

public partial class _jichu_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetValue();
        }
    }

    private void GetValue()
    {
        text_CarModelID.Value = "144803";
        text_Vin.Value = "D1111111111111121";
        text_EngineNumber.Value = "D1111111111111111";
        text_CarBrandNumber.Value = "京D11111";
        text_PurchasePrice.Value = "10000";
        text_RegisterDate.Value = "2016-6-1";
        text_InvoiceDate.Value = "2016-6-1";
        text_ClientName.Value = "客户姓名";
        text_ClientPhone.Value = "13520888087";
        text_PapersType.Value = "身份证";
        text_PapersNumber.Value = "222424198410065117";
        text_ClientAddress.Value = "客户地址";
        text_ClientPost.Value = "客户邮编";
        text_ClientEmail.Value = "客户邮箱";
        text_SingDate.Value = "2016-6-1";
        text_TakeEffectDate.Value = "2016-6-1";
        text_StartKM.Value = "40000";
        span_Enclosure.InnerText = "https://www.baidu.com/img/bd_logo.png|https://www.baidu.com/img/bd_logo1.png";
        text_SaleCompany.Value = "销售公司";
        text_ContractRemarks.Value = "合同备注";
    }

    #region 点击推送
    public void BtnSubmit(object sender, EventArgs e)
    {
        JsonModel model_Json = new JsonModel();
        model_Json.A = text_CarModelID.Value;
        model_Json.B = text_Vin.Value;
        model_Json.C = text_EngineNumber.Value;
        model_Json.D = text_CarBrandNumber.Value;
        model_Json.E1 = select_CarProvince.Value;
        model_Json.E2 = select_CarCity.Value;
        model_Json.E3 = select_CarDistrict.Value;
        model_Json.F = text_PurchasePrice.Value;
        model_Json.G = text_RegisterDate.Value;
        model_Json.H = text_InvoiceDate.Value;
        model_Json.I = text_ClientName.Value;
        model_Json.J = text_ClientPhone.Value;
        model_Json.K = text_PapersType.Value;
        model_Json.L = text_PapersNumber.Value;
        model_Json.M1 = select_ClientProvince.Value;
        model_Json.M2 = select_ClientCity.Value;
        model_Json.M3 = select_ClientDistrict.Value;
        model_Json.M4 = text_ClientAddress.Value;
        model_Json.N = text_ClientPost.Value;
        model_Json.N1 = text_ClientEmail.Value;
        model_Json.O = select_CarDegree.Value;
        model_Json.P = text_SingDate.Value;
        model_Json.Q = text_TakeEffectDate.Value;
        model_Json.R = text_StartKM.Value;
        model_Json.S = select_ProdutName.Value;
        model_Json.T = span_Enclosure.InnerText; //附件需绝对路径
        model_Json.U = text_SaleCompany.Value;
        model_Json.X = text_ContractRemarks.Value;
        model_Json.Y = "hina";
        model_Json.Z = "8A2399F8DC0C8F15CEB4";
        string result = PostData("http://localhost:6385/Web/GetPush_Zs.ashx", JsonConvert.SerializeObject(model_Json), Encoding.UTF8);
        ResultMSG model_ResultMSG = JsonConvert.DeserializeObject<ResultMSG>(result);
        //执行操作数据库
    }
    #endregion

    #region Post访问外部地址
    /// <summary>
    /// Post访问外部地址
    /// </summary>
    /// <param name="url">调用的URL</param>
    /// <param name="paraData">参数</param>
    /// <param name="encoding">编码格式(传空为默认的编码)</param>
    /// <returns></returns>
    public static string PostData(string url, string paraData, Encoding encoding)
    {
        if (string.IsNullOrEmpty(paraData))
        {
            return string.Empty;
        }
        try
        {
            byte[] dataPara = UTF8Encoding.UTF8.GetBytes(paraData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataPara.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(dataPara, 0, dataPara.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader strRead = null;
            if (encoding != null)
            {
                strRead = new StreamReader(response.GetResponseStream(), encoding);
            }
            else
            {
                strRead = new StreamReader(response.GetResponseStream());
            }
            string result = strRead.ReadToEnd();
            newStream.Close();
            strRead.Close();
            return result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    #region 接收post
    private string PostInput(HttpContext context)
    {
        try
        {
            System.IO.Stream s = context.Request.InputStream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            return builder.ToString();
        }
        catch (Exception ex)
        { throw ex; }
    }
    #endregion

}

public class JsonModel
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public string E1 { get; set; }
    public string E2 { get; set; }
    public string E3 { get; set; }
    public string F { get; set; }
    public string G { get; set; }
    public string H { get; set; }
    public string I { get; set; }
    public string J { get; set; }
    public string K { get; set; }
    public string L { get; set; }
    public string M1 { get; set; }
    public string M2 { get; set; }
    public string M3 { get; set; }
    public string M4 { get; set; }
    public string N { get; set; }
    public string N1 { get; set; }
    public string O { get; set; }
    public string P { get; set; }
    public string Q { get; set; }
    public string R { get; set; }
    public string S { get; set; }
    public string T { get; set; }
    public string U { get; set; }
    public string X { get; set; }
    public string Y { get; set; }
    public string Z { get; set; }
}

/// <summary>
/// 结果信息
/// </summary>
public class ResultMSG
{
    public string message { get; set; }
    public string htNO { get; set; }
    public DateTime htCreatTime { get; set; }
    public string htRemark { get; set; }
    public DateTime htEndTime { get; set; }
    public int htEndKM { get; set; }
    public float htMoney { get; set; }
    public string htContractKey { get; set; }
}

 

 

 

posted @ 2016-07-22 15:05  小奉手  阅读(419)  评论(0编辑  收藏  举报