2017-06-06 133 views
0

我寫了一些代碼以從遠程共享點列表中獲取項目。在獨立的Windows窗體應用程序中,它工作正常。但是,真正的任務是將其添加到現有的WCF Web服務中。與引用完全相同的網絡服務的網址完全相同的代碼拋出這個錯誤在事件查看器:Sharepoint 2010 Web服務GetListItems錯誤400錯誤請求

Exception: Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request."). Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request.")

這發生在這行代碼:

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

我會包括整個方法(用)一些名字改變,以保護無辜:))。請記住,它在一個獨立的贏取應用程序中工作正常。只有在WCF服務中,這是一個問題。對我來說更奇怪的是它掛起。只有在事件查看器中我看到這個錯誤。我有一個嘗試,我會認爲它會去抓。我甚至增加了一個額外的嘗試,只是爲了看和沒有骰子。這不是網址。這不是憑證。我甚至用硬編碼來確保這一點。也許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; 
     } 
+0

所以我從來沒有發現真正的問題。我結束了使用CSOM,它工作得很好,所以這是我一起去的。這仍然讓我感到不安。 – CMason

回答

0

我發現我意外地僅載入rtFaFedAuth餅乾,但不能從成功登入裝餅乾的其餘部分。這是事實證明,導致我所描述的錯誤。獲取會話中的所有cookie可以解決問題。