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();
        }



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