客户端以Post方式,json数据格式调用WCF,传入参数不是json,怎么自定义异常??

白日梦想jun 2018-05-30 10:49:49
1、WCF方法S_RCBD001规定请求和回应都是json格式,在方法内部用了try catch处理异常,返回具体信息;
2、若客户端调用为这样 SendHttpRequest("http://192.168.0.104:8081/Service1.svc", "S_RCBD001", JsonSerialize(""),"application/json");则没有问题;
但是若SendHttpRequest("http://192.168.0.104:8081/Service1.svc", "S_RCBD001", “”,"application/json");这样会报400的异常,怎么能让它不报400的异常,而是也返回自定义异常呢?
3.新定义以string为参数的S_RCBD002接口,接受参数后自己做反序列化,但是在客户端调用SendHttpRequest("http://192.168.0.104:8081/Service1.svc", "S_RCBD002", "", "text/plain"); 一直也是400的错误状态码。
非常急,希望有遇到过类似的问题的大佬们指导指导。

接口:
[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;

}


...全文
1039 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
百合杰 2018-05-30
  • 打赏
  • 举报
回复
一般的做法就是不管 因为你格式都错了 就别想着人家懂你的意思

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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