12,162
社区成员
发帖
与我相关
我的任务
分享
接口:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "/S_RCBD001")]
CommRes S_RCBD001(CommReq comm);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/S_RCBD002")]
CommRes S_RCBD002(String comm);
具体实现:
public CommRes S_RCBD001(CommReq commReq)
{
InsertMsg msg = new InsertMsg();
CommRes crs = new CommRes();
try
{
InputObj p = (InputObj)commReq.reqbody;
//具体业务操作 ……
crs.serialno = commReq.serialno;
crs.resptime = System.DateTime.Now.ToString("yyyyMMddHHmmssSSS");
crs.resultcode = "1";
crs.resultmsg = "成功";
crs.respbody = null;
}
catch (Exception ex)
{
crs.serialno = commReq.ToString();
crs.resptime = System.DateTime.Now.ToString("yyyyMMddHHmmssSSS");
crs.resultcode = "-1";
crs.resultmsg = "请求出错";
crs.respbody = null;
}
return crs;
}
public CommRes S_RCBD002(string com)
{
InsertMsg msg = new InsertMsg();
CommRes crs = new CommRes();
CommReq commReq = null;
try
{
commReq = jss.Deserialize<CommReq>(com);
InputObj p = (InputObj)commReq.reqbody;
//具体业务操作 ……
crs.serialno = commReq.serialno;
crs.resptime = System.DateTime.Now.ToString("yyyyMMddHHmmssSSS");
crs.resultcode = "1";
crs.resultmsg = "成功";
crs.respbody = null;
}
catch (Exception ex)
{
crs.serialno = commReq.ToString();
crs.resptime = System.DateTime.Now.ToString("yyyyMMddHHmmssSSS");
crs.resultcode = "-1";
crs.resultmsg = "请求出错";
crs.respbody = null;
}
return crs;
}
客户端调用方法:
public static string SendHttpRequest(string requestURI, string requestMethod, string json, string ContentType)
{
//json格式请求数据
string requestData = json;
//拼接URL
string serviceUrl = string.Format("{0}/{1}", requestURI, requestMethod);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//utf-8编码
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(requestData);
//post请求
myRequest.Method = "POST";
myRequest.Accept = "*/*";
myRequest.ContentLength = buf.Length;
//指定为json否则会出错
myRequest.ContentType = ContentType;
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数
myRequest.ServicePoint.Expect100Continue = false;
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
myRequest.CachePolicy = noCachePolicy;
string ReqResult = "";
Stream newStream = null;
HttpWebResponse myResponse = null;
StreamReader reader = null;
try
{
newStream = myRequest.GetRequestStream();
newStream.Write(buf, 0, buf.Length);
}
catch (Exception e)
{
}
finally
{
if (newStream != null) {
newStream.Close();
}
}
//获得接口返回值,格式
try
{
myResponse = (HttpWebResponse)myRequest.GetResponse();
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
ReqResult = reader.ReadToEnd();
}
catch (WebException e)
{
ReqResult = e.Message;
}
finally
{
if (reader != null) {
reader.Close();
}
if (myResponse != null) {
myResponse.Close();
}
}
return ReqResult;
}