WEB Service文件如下: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.Data.SqlClient; namespace Service { /// <summary> /// dbQuitGame 的摘要说明。 /// </summary> [WebService(Namespace="http://www.qgzxol.com")] public class dbQuitGame : System.Web.Services.WebService { public dbQuitGame() { //CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的 InitializeComponent(); connStr =System.Configuration.ConfigurationSettings.AppSettings["connStr"]; //connStr ="data source=QUITGAME;initial catalog=E-business;user id=sa;pwd=sa;packet size=4096"; } private String connStr; #region Component Designer generated code
1、在web service方法中通过try-catch抛出一个异常。
2、直接在调用方使用try-catch捕获异常,然后对异常做处理。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace Service
{
/// <summary>
/// dbQuitGame 的摘要说明。
/// </summary>
[WebService(Namespace="http://www.qgzxol.com")]
public class dbQuitGame : System.Web.Services.WebService
{
public dbQuitGame()
{
//CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
connStr =System.Configuration.ConfigurationSettings.AppSettings["connStr"];
//connStr ="data source=QUITGAME;initial catalog=E-business;user id=sa;pwd=sa;packet size=4096";
}
private String connStr;
#region Component Designer generated code
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{ } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion // WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键 [WebMethod] public DataSet Populate(String SQL)
{
SqlConnection myConn = new SqlConnection(connStr);
SqlDataAdapter myCmd = new SqlDataAdapter(SQL,myConn);
DataSet ds = new DataSet();
myCmd.Fill(ds,"dsTable");
return ds;
}
[WebMethod]
public String RunSQL(String SQL)
{
try
{
SqlConnection myConn = new System.Data.SqlClient.SqlConnection(connStr);
SqlCommand myCmd = new System.Data.SqlClient.SqlCommand(SQL,myConn);
myConn.Open();
myCmd.ExecuteNonQuery();
myConn.Close();
return("OK");
}
catch(Exception e)
{
String ret = "Exception:"+e.ToString();
return ret;
} }
}
}
在用 FORMS验证:
private void Ok_btn_Click(object sender, System.EventArgs e)
{
if(FormsAuthentication.Authenticate(userName.Text,userPwd.Text))
{
FormsAuthentication.RedirectFromLoginPage(userName.Text,false);
}
else
msg.Text = "ERR,Wrong UserName,Password!Input Admin/aspx or QuitGame/QuitGame";
}之前,没有任何问题,可以调用之进行 RunSQL()操作,但是在用了上面那个验证之后,没有调用 WEB Service 的操作均正常,但只要调用了WEB Service之后,进行RunSQL()操作的时候 就会有上 面的错误。请帮帮忙啊。
你用下面的方法,可以判定你的问题所在:
假设你调用的方法是WebService的MyMethod,你的web service代理是MyWebService, 在你的调用代码中:try
{
MyWebService myService=new MyWebService();
myService.MyMethod();
}
catch(Exception ex)
{
Response.Write(ex.Message)
}
System.Net.WebException: 请求失败,错误信息为: --
Object moved to here.
--. at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at Admin.localhost.dbQuitGame.RunSQL(String SQL) in G:\Docus\myweb\E_BUSINESS\Admin\Web 引用\localhost\Reference.cs:line 57 at Admin.addGoods.Add_btn_Click(Object sender, EventArgs e) in g:\docus\myweb\e_business\admin\addgoods.aspx.cs:line 85
直接输入 WEBService 路径,要求登陆,成功转到WEBService 路径,测试RunSQL方法,正常,这时候 再去调用WEBService 的地方,操作依然返回同样的出错信息。我的WEBService 和 WEB文件在同一工程,同一路径中
FormsAuthentication.GetRedirectUrl("*",false)为WEBService的时候
直接通过验证