ASMX Web服务和远程服务器返回错误:(500)内部服务器错误问题

时间:2015-03-31 13:00:12

标签: c# web-services

我开发了一个非常小的Web服务,并与我们的网站一起托管。我们的网络服务网址为http://www.bba-reman.com/Search/SearchDataIndex.asmx

网络服务代码

namespace WebSearchIndex
{
    #region SearchDataIndex
    /// <summary>
    /// SearchDataIndex is web service which will call function exist in another library for part data indexing
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]

    public class SearchDataIndex : System.Web.Services.WebService
    {
        //public AuthHeader ServiceAuth=null;
        public class AuthHeader : SoapHeader
        {
            public string Username;
            public string Password;
        }

        #region StartIndex
        /// <summary>
        /// this function will invoke CreateIndex function of SiteSearch module to reindex the data
        /// </summary>
        [WebMethod]
        public string StartIndex(AuthHeader auth)
        {
            string strRetVal = "";
            if (auth.Username == "Admin" && auth.Password == "Admin")
            {
                strRetVal = SiteSearch.CreateIndex(false);
            }
            else
            {
                SoapException se = new SoapException("Failed : Invalid credentials", 
                SoapException.ClientFaultCode,Context.Request.Url.AbsoluteUri,new Exception("Invalid credentials"));
                throw se;
            }
            return strRetVal;
        }
        #endregion
    }
    #endregion

}

当我使用HttpWebRequest类从我的win应用程序调用该Web服务时出现错误远程服务器返回错误:(500)内部服务器错误

这里是我的win应用程序的代码,我在这里调用Web服务

    string strXml = "";
    strXml = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><StartIndex xmlns='http://tempuri.org/' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><auth><Username>joy</Username><Password>joy</Password></auth></StartIndex></s:Body></s:Envelope>";
    string url = "http://www.bba-reman.com/Search/SearchDataIndex.asmx";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "text/xml";
    req.KeepAlive = false;
    req.ContentLength = strXml.Length;
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
    streamOut.Write(strXml);
    streamOut.Close();
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();

我只是无法理解此行何时执行StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());然后收到错误远程服务器返回错误:(500)内部服务器错误

无法理解我犯错误的地方。错误是在Web服务端的代码中还是在调用代码中?

帮我解决这个问题。感谢

1 个答案:

答案 0 :(得分:0)

我推荐的是以下内容: - 你摆脱了httpwebrequest。

  • 向您的应用程序添加一个服务引用,该引用将生成代理类。

- 最后添加此代码:

ServiceReference1.SearchDataIndexSoapClient Client = new ServiceReference1.SearchDataIndexSoapClient();
        ServiceReference1.AuthHeader authHead = new ServiceReference1.AuthHeader();
        authHead.Password = "Admin";
        authHead.Username = "Admin";

        Client.Endpoint.Binding.SendTimeout= new TimeSpan(0, 5, 00);
        Console.WriteLine(client.StartIndex(authHead));

应该是它