请各位知道的大师看看问题,
WCF 接口请求可以通 只是请求响应后出现这个问题,其它接口都可以请求通过;
错误信息:
System.Net.WebException: 基础连接已经关闭: 服务器关闭了本应保持活动状态的连接。
---> System.IO.IOException: 无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。
---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。
在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
在 System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
在 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.HttpWebRequest.GetResponse()
在 EHR.Core.WebServer.Request(String url, String param) 位置 D:\WorkCode\Eureka.Hospital.Solutions.EHR\Src\Servers\6 Foundation\EHR.Core\WebServer.cs:行号 47
在 EHR.Core.WebServer.GetWebServiceDateTime[T](String functionName, Object param) 位置 D:\WorkCode\Eureka.Hospital.Solutions.EHR\Src\Servers\6 Foundation\EHR.Core\WebServer.cs:行号 153
这个是请求方法:
private static string Request(string url, string param)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
var cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 40000;//40秒
request.Method = "POST";
request.ContentType = "application/json";
request.MaximumResponseHeadersLength = 200000;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0"; 200000
byte[] postdatabyte = Encoding.UTF8.GetBytes(param);
request.ContentLength = postdatabyte.Length;
Stream stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length); //设置请求主体的内容
stream.Close();
request.KeepAlive = false;
//接收响应
response = (HttpWebResponse)request.GetResponse();
Stream stream1 = response.GetResponseStream();
if (stream1 != null)
{
var sr = new StreamReader(stream1);
return sr.ReadToEnd();
}
return "";
}
返回对象都已经像这样序列化了:
[DataContract]
public class OrderInfoModel
{
/// <summary>
/// ID
/// </summary>
[DataMember]
public int OrderID { get; set; }
/// <summary>
/// CureNo
/// </summary>
[DataMember]
public string CureNo { get; set; }
接口服务关了???
/// <summary>
/// 访问服务并返回数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="functionName">调用的方法</param>
/// <param name="param">参数</param>
/// <returns></returns>
public static ResutEntity<T> GetWebService<T>(string functionName, object param = null) where T : new()
{
try
{
//拼接全路径
functionName = BasUrl + "/" + functionName.Replace(".", ".svc/");
string json = param == null ? "" : param.ToJson();
//请求数据
var mresult = Request(functionName, json);
var df = mresult.ToObject<ResutEntity<T>>();
return !string.IsNullOrEmpty(mresult) ? mresult.ToObject<ResutEntity<T>>() : default(ResutEntity<T>);
}
catch (Exception ex)
{
LogHelper.WriteError(functionName + "访问服务错误:" + ex.ToString());
return default(ResutEntity<T>);
}
}
请求方法:
public static List<OrderInfoModel> GetDrugOrderList(OrderParam param)
{
var reuslt = WebServer.GetWebServiceDateTime<OrderInfoModel>("OrdersService.GetDrugOrderList", param);
return reuslt.Data;
}
接口请求是可以请求通的,只是有时间的时候响应返回会报这个错误,没数据的时候请求正常,同时其它接口请求正常,数据端:
public void GetDrugOrderList(ResutEntity<OrderInfoModel> resultSearch, OrderParam oc)
{
using (var context = new CPOEEntities())
{
var whereStr = " where m.CURENO = '" + oc.CureNo + "' and m.STATUS != -1 ";
whereStr += GetDrugOrderWhere(oc);
var sqlStrAdmin = GetOrderListByStr() + whereStr;
var adminData = context.Database.SqlQuery<OrderInfoModel>(sqlStrAdmin).ToList();
resultSearch.Data = adminData;
resultSearch.Status = 0;
}
}
@心路独舞: 不是说你代码有问题,你问下接口提供方接口服务是不是关掉了
@华临天下: 没有,都在本地一个解决方案里面
@心路独舞: 没数据请求是正常的对吧?
我之前遇到过传的数据不对导致接口炸过? 特别是时间格式的一些数据,你看看数据格式
实在不行试试fiddler这个软件,查看错误信息,直接把错误甩给提供方看看是什么错误信息
@华临天下: 我来看看
private static string Request(string url, string param)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
var cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(url);
//request.Timeout = 15000;//40秒
request.Timeout = 40000;//40秒
request.Method = "POST";
request.ContentType = "application/json";
request.MaximumResponseHeadersLength = 200000;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0"; 200000
byte[] postdatabyte = Encoding.UTF8.GetBytes(param);
request.ContentLength = postdatabyte.Length;
Stream stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length); //设置请求主体的内容
stream.Close();
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
//接收响应
response = (HttpWebResponse)request.GetResponse();
Stream stream1 = response.GetResponseStream();
if (stream1 != null)
{
var sr = new StreamReader(stream1);
return sr.ReadToEnd();
}
return "";
}