C# Winform通过URL发送HTTP POST请求访问MES系统(oracle数据库)

花开花折 2018-10-14 11:06:40
需要:C# Winform通过URL发送HTTP POST请求访问MES系统(oracle数据库)

1:URL:http://127.0.0.1:8080/mes/service
2:查询接口:site=3000¶m=[{"name":"get_sfc","data":[{"model_no":"457542","project_no":"xxxx","batch_no":"27RC"}]}]
(1)参数说明:site=3000 工厂站点 param:参数为json格式字符串 name就是服务名称 data业务参数
(2)MES返回数据格式(Json)如下:
{"result":"sucess","message":"","data":
{
"tatal":"1","rows":[{"SFC":"45475454578"}]
}
}
(3)json字段说明:
result:接口交互状态,fail表示交互失败,sucess表示交互成功;message:result为fail有值:

1:我是应该利用HttpWebRequest 来实现与MES系统的对接,(向MES系统发URL+查询接口),类似下面的代码?????
	/// <summary>登录</summary>
/// <param name="url">请求地址</param>
/// <param name="date">请求参数</param>
/// <param name="method">请求方式(不是必需的)</param>
/// <param name="dataEncoding">编码格式(不是必需的)</param>
/// <returns></returns>
public static string loginAPI(string url, string date, method method, dataEncoding dataEncoding)
{
string res = string.Empty;//返回请求结果变量
string encodingType = dataEncoding.ToString() == "UTF8" ? "utf-8" : "GBK";//确定要使用的字符编号
Encoding encoding = Encoding.GetEncoding(encodingType);
//请求
WebRequest webRequest = null;
Stream postStream = null;
//响应
WebResponse webResponse = null;
StreamReader streamReader = null;
try
{
if (method == util.method.GET)
{
url = url + "?" + date;
}
//请求
webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
if (method == util.method.POST)
{
byte[] postDate = encoding.GetBytes(date);
webRequest.ContentLength = postDate.Length;
postStream = webRequest.GetRequestStream();
postStream.Write(postDate, 0, postDate.Length);
}
//响应
webResponse = webRequest.GetResponse();
streamReader = new StreamReader(webResponse.GetResponseStream(), encoding);
res = streamReader.ReadToEnd();
}
catch (WebException ex)
{
using (HttpWebResponse response = (HttpWebResponse)ex.Response)
{
using (Stream responseStream = response.GetResponseStream())
{
res = new StreamReader(responseStream).ReadToEnd();
}
}
}
catch (Exception)
{
}
finally
{
if (postStream != null)
{
postStream.Close();
}
if (streamReader != null)
{
streamReader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return res;
}


2:上面那中方式与通过数据库连接字符串访问Oracle的区别????类似如下代码
     string ConnectionString = "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;";
//创建一个新连接
OracleConnection conn=new OracleConnection(ConnectionString);
//以上两句也可以写成 OracleConnection conn=new OracleConnection "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;");
try
{
conn.Open();
//下面这句话,即使是select....返回一个int类型的数,也要按下面这么利用数据集去做
//不可能用一句话来实现
//注意从函数外面传递参数到sql语句中的写法
//比如传递AdNumber
//"selectyhbh from gspuser where yhbh='" + AdNumber + "'"
OracleCommand cmd = new OracleCommand("select * from FY", conn);
OracleDataAdapter oda = new OracleDataAdapter();
oda.SelectCommand = cmd;
DataSet ds = new DataSet();
oda.Fill(ds);
//如果这想要第一行第一列可以这么写ds.Tables[0].Rows[0][0]
gridControl1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
catch (Exception ee)
{
//如果有错误,输出错误信息
MessageBox.Show(ee.Message);
}
finally
{
//关闭连接
conn.Close();


...全文
2623 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
0 1看天下 2018-10-15
  • 打赏
  • 举报
回复
wcf webservice
张天星 2018-10-15
  • 打赏
  • 举报
回复
建议使用Post方式,winform到服务端应用层,服务端应用层操作数据库,然后返回数据给你。
而不是你直接去操作服务端数据库。
  • 打赏
  • 举报
回复
1、模拟http请求可以webrequest,也可以httpclient,或者用restsharp,是否正确测试下就行 2、如果是你自己的数据库,当然直连最方便,否则还是老老实实按api方式访问其它系统提供的接口,毕竟其可能还包含了其业务逻辑
  • 打赏
  • 举报
回复
winform是客户端,在用户本地你直接让它直连数据库的话,安全性较低,账户密码暴露的可能行高,如果是局域网的话还可以用用 通过接口方式不管对方系统接口后面怎么修改,改成了别的数据库进行了迁移,你都不需要管,你只要这个接口返回的数据跟一开始定的协议是一致的即可。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧