web service 开发过程中遇到的问题汇总:

接口只能在本地访问,放置服务器,其他人不能访问:

提示信息:The test form is only available for requests from the local machine.

解决方案:在web config文件中添加以下代码:

<configuration>
	  <system.web>
		<webServices>
		  <protocols>
			<add name="HttpGet"/>		<!--启用 HTTP GET-->
			<add name="HttpPost"/>		<!--启用 HTTP HttpPost-->
		  </protocols>
		</webServices>
	  </system.web>
</configuration>
配置之后,就可以在远程通过浏览器来调用webservice.asmx服务了,开发测试之后,为了安全,一定把该配置去掉!

调用WebService接口 json中文乱码;

原因:这是由于WebService默认的编码是“UTF-8”,当调用端和接收端用不同编码进行转换的时候,就会出现此类问题,如何解决这类问题呢,具体要根据你的环境而定。
解决方案:在web config文件中添加以下代码:

	<configuration>
	  <system.web>
		<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" />
	  </system.web>
	</configuration>

接口返回json类型:

需求:WebService默认的返回为XML 要返回json可以用json工具类把对象转为json字符串;
代码如下:

	[WebMethod]
        public void FindAccount(string phone, string code)
        {
            string ReturnStr = "";
            try
            {
                //判断CODE是否为有效值
                if (!Yasn.BLL.CarAccessoryBLL.IsQueryStringVaildata(code, "requestTime"))
                {
                    ReturnStr = "{\"status\":\"error\",\"message\":\"未提供或非法的Code参数\",\"result\":\"\"}";
                }
                else
                {
                    if (!Yasn.BLL.CarAccessoryBLL.IsQueryStringVaildata(phone, "phone"))
                    {
                        ReturnStr = "{\"status\":\"error\",\"message\":\"未提供或非法的MobilePhone参数\",\"result\":\"\"}";
                    }
                    else
                    {
                        ListMember model = Yasn.BLL.CarAccessoryBLL.GetCustomerInfoByPhone(phone);
                        if (null == model)
                        {
                            ReturnStr = "{\"status\":\"error\",\"message\":\"该手机号不存在\",\"result\":\"\"}";
                        }
                        else
                        {
                            List<ListReturnInfo> LM = new List<ListReturnInfo>();
                            ListReturnInfo LMember = new ListReturnInfo();


                            LMember.MobilePhone = model.MobilePhone.ToString();
                            LMember.AccountName = model.AccountName.ToString();
                            LMember.CounttyID = (Guid?)model.CounttyID;
                            LMember.Address = model.Address.ToString();
                            LMember.StoreType = model.StoreType.ToString();
                            LMember.StoreArea = model.StoreArea.ToString();
                            LMember.MainProduct = model.MainProduct.ToString();

                            LM.Add(LMember);
                            ReturnStr = "{\"status\":\"success\",\"message\":\"数据调用成功\",\"result\":" + JsonConvert.SerializeObject(LM) + "}";
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Yasn_Data.Utils.WriteLog(ex.Message);
                ReturnStr = "{\"status\":\"error\",\"message\":" + ex.Message + ",\"result\":\"\"}";
            }
            //return ReturnStr;					//利用return  返回的是xml;
            Context.Response.Write(ReturnStr);	//使用Context.Response.Write("结果");  这样返回的是join; 
            Context.Response.End();				//不要忘记加Context.Response.End();
        }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章