建立Web Service 接口及调用
WEB SERVICE 接口:
[WebMethod]
public string MaterialRequest(string jsonText)
{
string WorkNo;
string PN;
string PN_Name;
string Unit;
int Qty;
string Need_time="";
try
{
//jsonText格式: [{"WorkNo": "100001", "PN": "300003398", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020-1-20"},{"WorkNo": "100001", "PN": "300003397", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020-1-20"}]
JArray ja = (JArray)JsonConvert.DeserializeObject(jsonText);
//获取数值
if (ja.Count < 1) return "-1";
WorkNo = ja[0]["WorkNo"].ToString();
string sql = @"select * from WMS_KR_Task where taskno='" + WorkNo + "'";
DataTable dt = SqlHelper.ExecuteDataTable(sql);
if (dt == null) return "-1";
if (dt.Rows.Count > 0) return "1"; //已经存在
sql = "";
int counter = 0;
foreach (JObject jo in ja)
{
WorkNo = jo["WorkNo"].ToString();
PN = jo["PN"].ToString();
PN_Name = jo["PN_Name"].ToString();
Unit = jo["Unit"].ToString();
Qty = int.Parse(jo["Qty"].ToString());
Need_time = jo["Need_time"].ToString();
counter++;
sql += string.Format(@"INSERT INTO WMS_KR_Pick_Detail_From_MES(WorkNo, PN, PN_Name, Unit, Qty, Need_time) VALUES('{0}','{1}','{2}','{3}',{4},'{5}'); ", WorkNo, PN, PN_Name, Unit, Qty, Need_time);
}
sql += string.Format(@"insert into WMS_KR_TASK(TaskNo,TaskType,TaskStatus,TotalCount,ItemCount,FinishCount,RequiredTime) values('{0}',{1},{2},{3},{4},{5},'{6}');", WorkNo, (int)TaskTypeEnum.VerticalBank, (int)TaskStatusEnum.Initial, counter, counter, 0, Need_time);
SqlHelper.ExecSqlWithTrans1(sql);
return "0";
}
catch(Exception e)
{
return "-1";
}
}
C# Http Post 调用:
public void PostText()
{
string url = "http://localhost:80/WebService.asmx";
string method = "MaterialRequest";
string jsondata = "[{\"WorkNo\": \"100001\", \"PN\": \"300003398\", \"PN_Name\": \"电容器,呃呃按时\",\"Unit\": \"只\",\"Qty\": \"5000\",\"Need_time\": \"2020 - 1 - 20\"},{\"WorkNo\": \"100001\", \"PN\": \"300003397\", \"PN_Name\": \"电容器,呃呃按时\",\"Unit\": \"只\",\"Qty\": \"5000\",\"Need_time\": \"2020 - 1 - 20\"}]";
string result = HttpPostWebService(url, method, jsondata);
Console.WriteLine(result);
}
public string HttpPostWebService(string url, string method,string jsondata)
{
string result = string.Empty;
string param = string.Empty;
byte[] bytes = null;
Stream writer = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
param = HttpUtility.UrlEncode("jsonText") + "=" + HttpUtility.UrlEncode(jsondata);
bytes = Encoding.UTF8.GetBytes(param);
request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
try
{
writer = request.GetRequestStream(); //获取用于写入请求数据的Stream对象
}
catch (Exception ex)
{
return "-1";
}
writer.Write(bytes, 0, bytes.Length); //把参数数据写入请求数据流
writer.Close();
try
{
response = (HttpWebResponse)request.GetResponse(); //获得响应
}
catch (WebException ex)
{
return "-1";
}
// web service 返回的是XML ,所有需要这么读取
#region 这种方式读取到的是一个返回的结果字符串
Stream stream = response.GetResponseStream(); //获取响应流
XmlTextReader Reader = new XmlTextReader(stream);
Reader.MoveToContent();
result = Reader.ReadInnerXml();
#endregion
// 控制器方法返回的是字符串,不管是不是XML,不能用上面的方法读取,而只能用这个方法读取返回的字符。
#region 这种方式读取到的是一个Xml格式的字符串
//StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//result = reader.ReadToEnd();
#endregion
response.Dispose();
response.Close();
Reader.Dispose();
Reader.Close();
stream.Dispose();
stream.Close();
return result;
}
注意:下一行是否注释都能正常执行
下面是C#环境的测试界面:
如果按照上面的URL:http://localhost:6311/WebService.asmx?op=MaterialRequest 执行错误。
URL必须用: http://localhost:80/WebService.asmx/MaterialRequest
建立Web Service 接口及调用的更多相关文章
- 使用wsimport和JAX-WS调用Web Service接口
本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...
- ServiceStack Web Service 创建与调用简单示列
目录 ServiceStack 概念 ServiceStack Web Service 创建与调用简单示列 上篇文章介绍了ServiceStack是什么,本章进入主题,如何快速简单的搭建Service ...
- 统计随机数及临界值Web Service接口
(2017-02-04 银河统计) 统计函数API概念 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发 ...
- 在网页中运用统计Web Service接口
(2017-02-10 银河统计) 在"统计随机数及临界值Web Service接口"一文中介绍了常用统计分布四类Web Service接口(随机数.分位数.密度函数和累积分布函数 ...
- 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法
问题描述 通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题: XMLHttpRequest cannot load http:// ...
- 免费的天气Web Service接口
免费的天气Web Service接口 在android应用当中很多时候需要获取天气的信息,这里提供怎么获取天气信息: 1. http://www.ayandy.com/Service.asmx?wsd ...
- 使用JDK自带功能,实现一个简单的Web Service接口发布
万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...
- web service client端调用服务器接口
打开项目的web service client 其中wsdl URL http://www.51testing.com/html/55/67755-848510.html 去这里面查找一些公开的 ...
- web service接口 wsdl和asmx有什么区别
没有区别,只是后缀名的区别.Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立 ...
随机推荐
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe in position 45: illegal multibyte sequence
常见的一种解码错误如题目所示,下面介绍该错误的解决方法 (1).首先在打开文本的时候,设置其编码格式,如:open(‘1.txt’, encoding=’gbk’): (2).若(1)不能解决,可能是 ...
- jQuery使用ajax跨域请求获取数据
jQuery使用ajax跨域请求获取数据 跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的 ...
- 疫情下的传统商企自救|4个Tips搭建销量过亿直播间
新冠肺炎爆发以来,线下商企遭受巨大冲击.出于疫情防控需要,不少门店选择暂时停业:而消费者们更是响应号召.足不出户.这场疫情促使消费者的消费习惯和方式进一步转向线上订购转变,直播.短视频等领域逆势而起, ...
- Ubuntu18.04安装phpMyAdmin
1.使用apt自动安装 sudo apt install phpmyadmin 2.安装完成后,创建软链接到web根目录下(我的是/var/www/html/) sudo ln -s /usr/sha ...
- [BOI2003] Gem - 树形dp
结论 不同颜色数不会超过 \(O(\log n)\) 然后就是很简单的树形dp了 顺便复习一下树形dp怎么写 #include <bits/stdc++.h> using namespac ...
- vue报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...
- PyQt5-QDateEdit的简单使用
使用PyQt5开发图形界面,里面使用日期框,这里把这个QDateEdit组件命名为:beginDate from PyQt5.QtCore import QDate 1.初始化赋值,不设置则默认为20 ...
- PP: Reconstructing time series into a complex network to assess the evolution dynamics of the correlations among energy prices
Purpose detect the dynamics in time series of their correlation Methodology 1. calculate correlation ...
- TP5和TP3.2的使用区别
模板标签不一样: TP5 可在配置文件中自行定义自己喜欢的标签 TP5 使用双标签 如:{foreach} {/foreach} TP3 : <> TP5 :{} 调用数据表方式: M( ...
- Android 开发 SurfaceView 总结
Android中一种常见的自定义画UI接口类:SurfaceView.可以在异步线程中,完成相关数据更新. 首先介绍几个基本的定义,在其他知识中也会设计如下名词: 1.Paint 画笔,所有的图像.图 ...