【问题标题】:ASMX web service and The remote server returned an error: (500) Internal Server Error issueASMX Web 服务和远程服务器返回错误:(500)内部服务器错误问题
【发布时间】:2015-03-31 13:00:11
【问题描述】:

我开发了一个非常小的网络服务,它与我们的网站一起托管。我们的网络服务网址是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 应用程序代码

    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服务端代码还是调用代码?

帮我解决这个问题。谢谢

【问题讨论】:

    标签: c# web-services


    【解决方案1】:

    嗯,我建议如下: - 你摆脱了 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));
    

    应该是这样的

    【讨论】:

    • 什么是 ExtensionData; ?当我在代码中添加这一行时出现错误
    • 当此行执行 client.StartIndex(authHead) 然后我收到类似“内容类型 text/html; 响应消息的 charset=utf-8 与绑定的内容类型不匹配”的错误(文本/xml;字符集=utf-8)。”我猜内容类型有问题
    • 如何通过添加引用在客户端调用服务时设置内容类型?还是我需要在 Web 服务代码中设置任何内容类型?
    • 只是摆脱扩展数据对于错误,您似乎无权执行该服务,因此如果我不是接收肥皂 xml 响应,您将收到一个 HTML 错误页面猜对了,您应该会看到响应中包含的 HTMl 页面
    • 谁能告诉我我需要在 Web 服务代码级别做什么,结果内容类型响应将是 text/xml 而不是 text/html。