WebForm通过webservice接口远程读写工控机视频车辆检测器配置文件的方法
作者:互联网
视频检测管理平台远程读取信号机配置文件的方法:
一、浏览器地址栏输入:http://XXX.XXX.X.XXX:XXXX/config 即可获取信号机的配置文件内容,以json格式显示,在平台开发中是不能通过ajax读取这个URL中的内容的,因为跨域问题,浏览器拒绝访问。
二、解决读取跨域问题:迂回战术解决,即在页面通过ajax提交请求后端的WEBMETHOD方法,通过后端访问上面的URL,即可跳过浏览器取到值然后返回页面。远程读取配置文件的接口URL:http://XXX.XXX.X.XXX:XXXX/config,此接口需要通过GET方式传递数据。
前端代码:
//通过webservice获取数据
function getWebData(ipstr) {
var jg;
$.ajax({
type: "post",
url: "basicSet_page.aspx/GetWebserviceData",//格式为"文件名(含后缀)/方法名"
data: "{URL:'" + ipstr + "'}",
contentType: "application/json; charset=utf-8",//发送信息至服务器时内容编码类型
dataType: "json",//服务器返回的数据类型
async: false,
success: function (data) {
//var hh = JSON.parse(data);
jg = JSON.parse(data.d);
}, error: function () {
alert("数据获取失败!");
}
});
return jg;
}
三、后端远程读取WebService接口的方法:
[WebMethod]
public static string GetWebserviceData(string URL)
{
string retXml = "";
try
{
//如果有参数传入则可放开屏蔽进行拼接后编码写入请求流
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "GET";//此处根据webservice的要求来设置,允许GET、POST两种类型
request.UserAgent = "Opera/9.25 (Windows NT 6.0; U; en)";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 60000;
//参数拼接成字符串,最后编码
StringBuilder sb = new StringBuilder();
//foreach (string k in Pars.Keys)
//{
// if (sb.Length > 0)
// {
// sb.Append("&");
// }
// sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
//}
byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
// 把编码后的参数写入请求流中
//request.ContentLength = data.Length;
//Stream writer = request.GetRequestStream();
//writer.Write(data, 0, data.Length);
//writer.Close();
// 读取结果,创建XmlDocument对象,对象加载结果
//var response = request.GetResponse();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
retXml = sr.ReadToEnd();
//response.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
sr.Close();
}
return retXml;
}
catch (Exception ex)
{
ex.StackTrace.ToString();
System.Diagnostics.Trace.WriteLine(ex.Message);
return retXml;
}
}
四、远程写配置文件的接口URL:http://XXX.XXX.X.XXX:XXXX/camconfig,此接口需要通过POST方式传递数据。
五、前端页面数据提交webservice接口远程写信号机配置文件的方法:
function save() {
var jg;
var cameraNameStr = $("#cameraName").textbox('getValue');
var IPAddressStr = $("#IPAddress").textbox('getValue');
var portStr = $("#port").textbox('getValue');
var UserNameStr = $("#UserName").textbox('getValue');
var passwordStr = $("#Password").textbox('getValue');
var modelStrTemp = $("#model").combobox('getText');
var modelStr = 0;
switch (modelStrTemp) {
case "--":
modelStr = 0;
break;
case "海康":
modelStr = 1;
break;
case "大华":
modelStr = 2;
break;
case "宇视科技":
modelStr = 3;
break;
case "华安":
modelStr = 4;
break;
case "信路威":
modelStr = 5;
break;
case "测试":
modelStr = 6;
break;
}
var codecStrTemp = $("#codec").combobox('getText');
var codecStr = 0;
switch (codecStrTemp) {
case "主码流":
codecStr = 0;
break;
case "辅码流":
codecStr = 1;
break;
}
var inputModeStr = $("#inputMode").combobox('getText');
var inputChannelStr = $("#inputChannel").textbox('getValue');
var validStr = $("#valid")[0].checked ? 1 : 0;
var camid = cameraId + 1;
//前端组织json字符串的方法,注意这只是一个json字符串,不是json对象,ajax提交时要设置dataType:"text",否则无法访问后端方法,此处是按text方式,传入后台一个json字符串,而不是一个对象,后端要用string来接收这个json字符串,这种方法,不影响其参数的传递
var jsonString = '{"camid": ' + camid + ',"camname": "' + cameraNameStr + '", "camip": "' + IPAddressStr + '", "camport": "' + portStr + '", "camuser": "' + UserNameStr + '", "campassword": "' + passwordStr + '", "cammodel": ' + modelStr + ', "camcodec": ' + codecStr + ', "inputmode": "' + inputModeStr + '", "inputchannel": ' + inputChannelStr + ', "camvalid": ' + validStr + '}';
$.ajax({
type: "post",
url: "cameraconfig.aspx/SendDataToWebservice",//格式为"文件名(含后缀)/方法名"
contentType: "application/json; charset=utf-8",//发送信息至服务器时内容编码类型
dataType: "text",//服务器返回的数据类型
data: "{URL:'" + camconfigIp + "',camObj: '" + jsonString + "'}",
async: false,
success: function (data) {
var hh = JSON.parse(data);
var jg = JSON.parse(hh.d)
$.messager.alert('信息提示', '数据保存成功!', "info", function () {
parent.closeCameraConfigWin();
});
}, error: function () {
alert("数据获取失败!");
}
});
return jg;
}
五、后端远程写信号机配置文件的WEBMETHOD方法:
[WebMethod]
public static string SendDataToWebservice(string URL,string camObj)
{
string retXml = "";
try
{
//如果有参数传入则可放开屏蔽进行拼接后编码写入请求流
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";//此处根据webservice的要求来设置,允许GET、POST两种类型
request.UserAgent = "Opera/9.25 (Windows NT 6.0; U; en)";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 60000;
//参数拼接成字符串,最后编码
StringBuilder sb = new StringBuilder();
//foreach (string k in Pars.Keys)
//{
// if (sb.Length > 0)
// {
// sb.Append("&");
// }
// sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
//}
sb.Append(camObj);
byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
//把编码后的参数写入请求流中
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close();
// 读取结果,创建XmlDocument对象,对象加载结果
//var response = request.GetResponse();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
retXml = sr.ReadToEnd();
//response.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
sr.Close();
}
return retXml;
}
catch (Exception ex)
{
ex.StackTrace.ToString();
System.Diagnostics.Trace.WriteLine(ex.Message);
return retXml;
}
}
秋风写于淄博,业务联系与技术交流:Q:375172665
标签:工控机,webservice,配置文件,request,var,sb,data,response,string 来源: https://blog.csdn.net/hmwz0001/article/details/122366410