winform 端调用
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Hashtable hashtable = new Hashtable();
hashtable.Add("TaskNo", DateTime.Now.Ticks.ToString() + "1");
hashtable.Add("ExecAgv", 1);
var data =PostWebService("http://localhost:53283/AgvDispatchService.asmx", "HelloWorld", hashtable);
textBox1.Text = data;
}
/// <summary>
/// 需要WebService支持Post调用
/// </summary>
public static string PostWebService(String URL, String MethodName, Hashtable ht)
{
string result="";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// request.ContentType = "application/json;charset=utf-8";
// 凭证
request.Credentials = CredentialCache.DefaultCredentials;
//超时时间
//request.Timeout = 10000;
var PostStr = HashtableToJson(ht);
byte[] data = System.Text.Encoding.UTF8.GetBytes(PostStr);
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close();
try
{
var response = request.GetResponse();
var stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
String retXml = sr.ReadToEnd();
//将string 转化为 XmlDocument对象的xml格式
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(retXml);
XmlNode root= xmlDoc.DocumentElement;
result = root.FirstChild.InnerText;
sr.Close();
}
catch (WebException ex)
{
result = ex.Response.ToString();
throw;
}
return result;
}
private static String HashtableToPostData(Hashtable ht)
{
StringBuilder sb = new StringBuilder();
foreach (string k in ht.Keys)
{
if (sb.Length > 0)
{
sb.Append("&");
}
sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(ht[k].ToString()));
}
return sb.ToString();
}
private static char[] HashtableToJson(Hashtable hashtable)
{
string json = JsonConvert.SerializeObject(hashtable);
char[] charArray = json.ToCharArray();
return charArray;
}
}
}
web service
using Dapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Web;
using System.Web.Services;
using WebService_Saimo.Models;
namespace WebService_Saimo
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class AgvDispatchService : System.Web.Services.WebService
{
[WebMethod]
public ResultInfo HelloWorld()
{
ResultInfo resultInfo = new ResultInfo();
try
{
Stream stream = HttpContext.Current.Request.InputStream;//获得json 字符流
//还原数据流
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
string jsonText = Encoding.UTF8.GetString(data);
TaskInfo taskInfo = JsonSerializer.Deserialize<TaskInfo>(jsonText);
string sql = "INSERT INTO Test_1 (test) Values (@CustomerName);";
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
var affectedRows = connection.Execute(sql, new { CustomerName = taskInfo.ExecAgv.ToString() });
if(affectedRows>0)
{
resultInfo.Result = true;
resultInfo.Message = "json";
}
Console.WriteLine(affectedRows);
// var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
}
}
catch (Exception ex)
{
resultInfo.Result = false;
resultInfo.Message = ex.Message;
}
return resultInfo;
}
[WebMethod]
public ResultInfo HelloWorld121(object obj)
{
ResultInfo resultInfo = new ResultInfo();
resultInfo.Result = true;
resultInfo.Message = "json";
return resultInfo;
}
}
}