C#后台调用webapi

Stella981
• 阅读 697
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ZSWinForm
{
    public class ApiHelper
    {
        /// <summary>
        /// 调用api返回json
        /// </summary>
        /// <param name="url">api地址</param>
        /// <param name="context">接收参数</param>
        /// <returns></returns>
        public static string HttpApi(string uri, string context)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Proxy = null;
            request.Method = "POST";

            //request.Accept = "*/*";
            request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentType = "text/html;charset=gbk";
            byte[] buffer = Encoding.Default.GetBytes(context);
            request.ContentLength = buffer.Length;
            //WebRequest.GetSystemWebProxy();
            request.GetRequestStream().Write(buffer, 0, buffer.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response == null) return null;
            StreamReader sr = new StreamReader(response.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZSWinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            string ROW_ID = txt_ROW_ID.Text;
            string DEALER_ID = txt_DEALER_ID.Text;
            string AMOINT_SUMMARY = txt_AMOUNT_SUMMARY.Text;
            string COLLECT_NUMBER = txt_COLLECT_NUMBER.Text;
            string CREATE_DT = txt_CREATE_DT.Text;
            string LASTUPDATE_DT = txt_LASTUPDATE_DT.Text;
            string CREATOR_ID = txt_CREATOR_ID.Text;
            string DELIVERY_WAREHOUSE = txt_DELIVERY_WAREHOUSE.Text;
            string INDENT_FLAG = txt_INDENT_FLAG.Text;
            string INDENT_NUMBER = txt_INDENT_NUMBER.Text;
            string INDENT_REMARK = txt_INDENT_REMARK.Text;
            string LASTUPDATOR_ID = txt_LASTUPDATOR_ID.Text;
            string SHIPPING_ADDRESS = txt_SHIPPING_ADDRESS.Text;
            string SUPPLIER_ID = txt_SUPPLIER_ID.Text;
            string ORDERTYPE = txt_ORDERTYPE.Text;
            string OU_TYPE = txt_OU_TYPE.Text;

            string ROW_IDS = txt_ROW_IDS.Text;
            string INDENT_APPLY_ID = txt_INDENT_APPLY_ID.Text;
            string PRODUCT_ID = txt_PRODUCT_ID.Text;
            string QUANTITY = txt_QUANTITY.Text;
            string ROW_MONEY = txt_ROW_MONEY.Text;
            string MEASURE = txt_MEASURE.Text;
            string PRODUCT_NO = txt_PRODUCT_NO.Text;
            string BASE_PRICE = txt_BASE_PRICE.Text;
            string QUANTITYSEND = txt_QUANTITYSEND.Text;
            string QUANTITYRECEIVED = txt_QUANTITYRECEIVED.Text;
            string CREATE_DTS = txt_CREATE_DTS.Text;

            string ROW_IDS2 = txt_ROW_IDS2.Text;
            string INDENT_APPLY_ID2 = txt_INDENT_APPLY_ID2.Text;
            string PRODUCT_ID2 = txt_PRODUCT_ID2.Text;
            string QUANTITY2 = txt_QUANTITY2.Text;
            string ROW_MONEY2 = txt_ROW_MONEY2.Text;
            string MEASURE2 = txt_MEASURE2.Text;
            string PRODUCT_NO2 = txt_PRODUCT_NO2.Text;
            string BASE_PRICE2 = txt_BASE_PRICE2.Text;
            string QUANTITYSEND2 = txt_QUANTITYSEND2.Text;
            string QUANTITYRECEIVED2 = txt_QUANTITYRECEIVED2.Text;
            string CREATE_DTS2 = txt_CREATE_DTS2.Text;
            //主要是下面这几行
            string uri = "http://localhost:5706//api/DBinfo/UpPDAData";
            string context = "requestContent={\"ROW_ID\":\"" + ROW_ID + "\",\"DEALER_ID\":\"" + DEALER_ID + "\",\"AMOUNT_SUMMARY\":" + AMOINT_SUMMARY + ",\"COLLECT_NUMBER\":\"" + COLLECT_NUMBER + "\",\"CREATE_DT\":\"" + CREATE_DT + "\",\"LASTUPDATE_DT\":\"" + LASTUPDATE_DT + "\",\"CREATOR_ID\":\"" + CREATOR_ID + "\",\"DELIVERY_WAREHOUSE\":\"" + DELIVERY_WAREHOUSE + "\",\"INDENT_FLAG\":\"" + INDENT_FLAG + "\",\"INDENT_NUMBER\":\"" + INDENT_NUMBER + "\",\"INDENT_REMARK\":\"" + INDENT_REMARK + "\",\"LASTUPDATOR_ID\":\"" + LASTUPDATOR_ID + "\",\"SHIPPING_ADDRESS\":\"" + SHIPPING_ADDRESS + "\",\"SUPPLIER_ID\":\"" + SUPPLIER_ID + "\",\"ORDERTYPE\":\"" + ORDERTYPE + "\",\"OU_TYPE\":\"" + OU_TYPE + "\",\"ZS_DBMXInfos\":[{\"ROW_ID\":\"" + ROW_IDS + "\",\"INDENT_APPLY_ID\":\"" + INDENT_APPLY_ID + "\",\"PRODUCT_ID\":\"" + PRODUCT_ID + "\",\"QUANTITY\":\"" + QUANTITY + "\",\"ROW_MONEY\":\"" + ROW_MONEY + "\",\"MEASURE\":\"" + MEASURE + "\",\"PRODUCT_NO\":\"" + PRODUCT_NO + "\",\"BASE_PRICE\":\"" + BASE_PRICE + "\",\"QUANTITYSEND\":\"" + QUANTITYSEND + "\",\"QUANTITYRECEIVED\":\"" + QUANTITYRECEIVED + "\",\"CREATE_DT\":\"" + CREATE_DTS + "\"},{\"ROW_ID\":\"" + ROW_IDS2 + "\",\"INDENT_APPLY_ID\":\"" + INDENT_APPLY_ID2 + "\",\"PRODUCT_ID\":\"" + PRODUCT_ID2 + "\",\"QUANTITY\":\"" + QUANTITY2 + "\",\"ROW_MONEY\":\""+ ROW_MONEY2 + "\",\"MEASURE\":\""+ MEASURE2 + "\",\"PRODUCT_NO\":\""+ PRODUCT_NO2 + "\",\"BASE_PRICE\":\""+ BASE_PRICE2 + "\",\"QUANTITYSEND\":\""+ QUANTITYSEND2 + "\",\"QUANTITYRECEIVED\":\""+ QUANTITYRECEIVED2 + "\",\"CREATE_DT\":\""+ CREATE_DTS2 + "\"}]}";
            var str = ApiHelper.HttpApi(uri,context);
            txt_result.Text = str;

        }

       
    }
}

using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace ZKT.Mobile.Web.Controllers
{
    public class DBInfoController : BaseController
    {
        string connstr = ConfigurationManager.ConnectionStrings["ZKT"].ConnectionString;
        [HttpPost]
        public HttpResponseMessage UpPDAData()
        {
            string rev = HttpContext.Current.Request.Params["requestContent"];
            ZS_DBInfo i = JsonConvert.DeserializeObject<ZS_DBInfo>(rev);
            BaseResponse response = new BaseResponse();
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            SqlTransaction trans = conn.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBInfo]
                                                           ([ROW_ID]
                                                           ,[DEALER_ID]
                                                           ,[AMOUNT_SUMMARY]
                                                           ,[COLLECT_NUMBER]
                                                           ,[CREATE_DT]
                                                           ,[LASTUPDATE_DT]
                                                           ,[CREATOR_ID]
                                                           ,[DELIVERY_WAREHOUSE]
                                                           ,[INDENT_FLAG]
                                                           ,[INDENT_NUMBER]
                                                           ,[INDENT_REMARK]
                                                           ,[LASTUPDATOR_ID]
                                                           ,[SHIPPING_ADDRESS]
                                                           ,[SUPPLIER_ID]
                                                           ,[ORDERTYPE]
                                                           ,[OU_TYPE])
                                                     VALUES
                                                           (@ROW_ID
                                                           ,@DEALER_ID
                                                           ,@AMOUNT_SUMMARY
                                                           ,@COLLECT_NUMBER
                                                           ,@CREATE_DT
                                                           ,@LASTUPDATE_DT
                                                           ,@CREATOR_ID
                                                           ,@DELIVERY_WAREHOUSE
                                                           ,@INDENT_FLAG
                                                           ,@INDENT_NUMBER
                                                           ,@INDENT_REMARK
                                                           ,@LASTUPDATOR_ID
                                                           ,@SHIPPING_ADDRESS
                                                           ,@SUPPLIER_ID
                                                           ,@ORDERTYPE
                                                           ,@OU_TYPE);
                                                             select @@IDENTITY As ID;";
                cmd.CommandType = CommandType.Text;
                SqlParameter[] parp = new SqlParameter[]{
                              new SqlParameter("@ROW_ID",i.ROW_ID),
                              new SqlParameter("@DEALER_ID",i.DEALER_ID),
                              new SqlParameter("@AMOUNT_SUMMARY",i.AMOUNT_SUMMARY),
                              new SqlParameter("@COLLECT_NUMBER",i.COLLECT_NUMBER),
                              new SqlParameter("@CREATE_DT",i.CREATE_DT),
                              new SqlParameter("@LASTUPDATE_DT",i.LASTUPDATE_DT),
                              new SqlParameter("@CREATOR_ID",i.CREATOR_ID),
                              new SqlParameter("@DELIVERY_WAREHOUSE",i.DELIVERY_WAREHOUSE),
                              new SqlParameter("@INDENT_FLAG",i.INDENT_FLAG),
                              new SqlParameter("@INDENT_NUMBER",i.INDENT_NUMBER),
                              new SqlParameter("@INDENT_REMARK",i.INDENT_REMARK),
                              new SqlParameter("@LASTUPDATOR_ID",i.LASTUPDATOR_ID),
                              new SqlParameter("@SHIPPING_ADDRESS",i.SHIPPING_ADDRESS),
                              new SqlParameter("@SUPPLIER_ID",i.SUPPLIER_ID),
                              new SqlParameter("@ORDERTYPE",i.ORDERTYPE),
                              new SqlParameter("@OU_TYPE",i.OU_TYPE)
                        };
                cmd.Parameters.AddRange(parp);
                int autono = Convert.ToInt32(cmd.ExecuteScalar());
                foreach (var a in i.ZS_DBMXInfos)
                {
                    cmd.Parameters.Clear();
                    cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBMXInfo]
                                                           ([ROW_ID]
                                                           ,[INDENT_APPLY_ID]
                                                           ,[PRODUCT_ID]
                                                           ,[QUANTITY]
                                                           ,[ROW_MONEY]
                                                           ,[MEASURE]
                                                           ,[PRODUCT_NO]
                                                           ,[BASE_PRICE]
                                                           ,[QUANTITYSEND]
                                                           ,[QUANTITYRECEIVED]
                                                           ,[CREATE_DT]
                                                           ,[Autono])
                                                     VALUES
                                                           (@ROW_ID
                                                           ,@INDENT_APPLY_ID
                                                           ,@PRODUCT_ID
                                                           ,@QUANTITY
                                                           ,@ROW_MONEY
                                                           ,@MEASURE
                                                           ,@PRODUCT_NO
                                                           ,@BASE_PRICE
                                                           ,@QUANTITYSEND
                                                           ,@QUANTITYRECEIVED
                                                           ,@CREATE_DT
                                                           ,@Autono)";
                    cmd.CommandType = CommandType.Text;
                    SqlParameter[] paras2 = new SqlParameter[]{
                                    new SqlParameter("@ROW_ID",a.ROW_ID),
                                    new SqlParameter("@INDENT_APPLY_ID",a.INDENT_APPLY_ID),
                                    new SqlParameter("@PRODUCT_ID",a.PRODUCT_ID),
                                    new SqlParameter("@QUANTITY",a.QUANTITY),
                                    new SqlParameter("@ROW_MONEY",a.ROW_MONEY),
                                    new SqlParameter("@MEASURE",a.MEASURE),
                                    new SqlParameter("@PRODUCT_NO",a.PRODUCT_NO),
                                    new SqlParameter("@BASE_PRICE",a.BASE_PRICE),
                                    new SqlParameter("@QUANTITYSEND",a.QUANTITYSEND),
                                    new SqlParameter("@QUANTITYRECEIVED",a.QUANTITYRECEIVED),
                                    new SqlParameter("@CREATE_DT",a.CREATE_DT),
                                    new SqlParameter("@Autono",autono)
                            };
                    cmd.Parameters.AddRange(paras2);
                    cmd.ExecuteNonQuery();
                }

                trans.Commit();
                response.Status = 1;
                response.Message = "添加成功!";
            }
            catch (Exception e)
            {
                trans.Rollback();
                response.Status = 0;
                response.Message = "添加失败!";
            }
            finally
            {
                conn.Close();
                trans.Dispose();
                conn.Dispose();
            }
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这