Sharepoint 2010 Web服务GetListItems错误400错误请求

时间:2017-06-06 17:44:10

标签: c# .net wcf sharepoint

我编写了一些代码来从远程共享点列表中获取项目。在独立的Windows窗体应用程序中,它工作正常。但是,真正的任务是将其添加到现有的WCF Web服务中。具有完全相同的Web服务URL的完全相同的代码会在事件查看器中引发此错误:

  

异常:捕获异常:System.dll中的“System.Net.WebException”   (“远程服务器返回错误:(400)错误请求。”)。例外   catch:System.dll中的“System.Net.WebException”(“远程服务器”   返回错误:(400)错误请求。“)

这发生在这行代码上:

XmlNode nodeListItems =
                        listService.GetListItems
                        (listName, string.Empty, query, viewFields, "0", queryOptions, null);

我将在下面包含整个方法(更改一些名称以保护无辜的:)。请记住,它在一个独立的赢取应用程序中工作正常。仅在WCF服务中这是一个问题。对我来说更奇怪的是它挂起了。只有在事件查看器中才能看到此错误。我有一个尝试捕获,我会认为它会去捕获。我甚至添加了额外的尝试捕获只是为了看到和没有骰子。这不是URL。这不是证书。我甚至在某一点上对它们进行了硬编码,以确保这一点。也许WCF中有一些隐藏的配置设置?

感谢任何帮助。这是代码:

private string GetPhoneNumber(string AccountNumber)
        {
            var retvalue = "";

            try
            {
                Lists listService = new Lists();

                /*Authenticate the current user by passing their default 
                credentials to the Web service from the system credential 
                cache. */

                //listService.Credentials = new NetworkCredential(sUsername, sPassword, domain);
                listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

                // listService.Credentials = new System.Net.NetworkCredential("un", "pw");
                /*Set the Url property of the service for the path to a subsite. 
                Not setting this property will return the lists in the root Web site.*/
                listService.Url =
                "somesharepointsite/_vti_bin/Lists.asmx";

                /* Instantiate an XmlDocument object */
                XmlDocument xmlDoc = new XmlDocument();

                /* Assign values to the string parameters of the GetListItems method, 
                using GUIDs for the listName and viewName variables. For listName, 
                using the list display name will also work, but using the list GUID 
                is recommended. For viewName, only the view GUID can be used. 
                Using an empty string for viewName causes the default view to be used.*/
                string listName = "Telephone Numbers";

                /*Use the CreateElement method of the document object to create 
                elements for the parameters that use XML.*/
                XmlElement query = xmlDoc.CreateElement("Query");
                XmlElement viewFields =
                    xmlDoc.CreateElement("ViewFields");
                XmlElement queryOptions =
                    xmlDoc.CreateElement("QueryOptions");

                /*To specify values for the parameter elements (optional), assign 
                CAML fragments to the InnerXml property of each element.*/
                query.InnerXml = "<Where>" +
                                    "<And><Gt><FieldRef Name = 'ID'/><Value Type='Counter'>0</Value></Gt>" +
                                    "<Eq><FieldRef Name = 'Title'/><Value Type = 'Text'>" + AccountNumber + "</Value></Eq>" +
                                    "</And>" +
                                 "</Where>";
                viewFields.InnerXml = "";
                queryOptions.InnerXml = "";

                /* Declare an XmlNode object and initialize it with the XML response 
                from the GetListItems method. The last parameter specifies the GUID 
                of the Web site containing the list. Setting it to null causes the 
                Web site specified by the Url property to be used.*/

                try
                {
                    XmlNode nodeListItems =
                        listService.GetListItems
                        (listName, string.Empty, query, viewFields, "0", queryOptions, null);

                    if (nodeListItems != null)
                    {
                        /*Loop through each node in the XML response and get data.*/
                        foreach (XmlNode listItem in nodeListItems)
                        {
                            var Phone = "No phone number";

                            if (listItem.Name == "rs:data")
                            {
                                for (int f = 0; f < listItem.ChildNodes.Count; f++)
                                {
                                    if (listItem.ChildNodes[f].Name == "z:row")
                                    {
                                        Phone = listItem.ChildNodes[f].Attributes["ows_Telephone"].Value.TrimEnd('0', '.');
                                        retvalue = Phone;
                                    }

                                }
                            }
                        }
                    }

                    if (retvalue == "") retvalue = "No result found.";
                }
                catch (WebException ex)
                {
                    retvalue = ex.Message;
                }
            }
            catch (WebException ex)
            {
                retvalue = ex.Message;
            }
            catch (Exception ex)
            {
                retvalue = ex.Message;
            }

            return retvalue;
        }

1 个答案:

答案 0 :(得分:0)

我发现我偶然只加载了rtFaFedAuth个Cookie,但没有从成功登录中加载其余的Cookie。结果导致我所描述的错误。从会话中获取所有cookie可以解决问题。