繁体   English   中英

服务器无法处理请求。 --->请求失败,HTTP状态为401:未经授权

[英]Server was unable to process request. ---> The request failed with HTTP status 401: Unauthorized

我有一个Web服务,我已经编写了将值添加到共享点列表的方法,可以在localmachine上调用该Web服务。 但是当我将其部署到服务器时,状态为401:未经授权。

谁能告诉我为什么我收到401代理错误,但我尝试在Web配置中添加代理标签却无法正常工作。

 [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Req : System.Web.Services.WebService
{

    [WebMethod]
    public Guid Add()
    {

        string uniqueGuid = string.Empty;
        string[] uniqueID;
    try 
    {           
                CrmList.Lists list = new CrmList.Lists();
                list.PreAuthenticate = true;


                #region getvalue
                list.Credentials = System.Net.CredentialCache.DefaultCredentials;
                XmlNode ndLists = list.GetListCollection();


                //XmlNode test = list.GetList(listName);

                list.Url = "http://site/_vti_bin/Lists.asmx";


                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

                string listName = Properties.Settings.Default.ListGuid;
                string viewName = Properties.Settings.Default.ViewGuid; 
                string rowLimit = Properties.Settings.Default.RowLimit;

                System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
                System.Xml.XmlElement viewFields =
                    xmlDoc.CreateElement("ViewFields");
                System.Xml.XmlElement queryOptions =
                    xmlDoc.CreateElement("QueryOptions");


                query.InnerXml = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";

                viewFields.InnerXml = @"<FieldRef Name='ID' />";


                queryOptions.InnerXml = "";


                System.Xml.XmlNode nodeListItems = list.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, "09bdac52-8897-41ee-8a0e-df5fdba617e9");





                #endregion

                string listdata=string.Empty;
                string InvoiceType =string.Empty;

                    listdata= @"<Method ID=""1"" Cmd=""New"">";

                    listdata += @"<Field Name=""ID""></Field>";

                    listdata += @"</Method>";

                    System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument() ;

                    System.Xml.XmlElement xbatch = xdoc.CreateElement("Batch");
                    xbatch.SetAttribute("OnError", "Return");
                    xbatch.InnerXml = listdata;

                    try
                    {
                        list.UpdateListItems(listName, xbatch);

                    }
                    catch (Exception ex)
                    {

                    }



        #region getguid
                    System.Xml.XmlNode currentListValues = list.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions,Properties.Settings.Default.WebGuid);
                    string test = string.Empty;
                    XmlDocument xdoc1 = new XmlDocument();
                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(xdoc1.NameTable);
                    nsmanager.AddNamespace("ns1", "http://schemas.microsoft.com/sharepoint/soap");
                    xdoc1.LoadXml(currentListValues.InnerXml);
                    System.Xml.XmlElement root = xdoc1.DocumentElement;
                    List<string> str1 = new List<string>();
                    XmlAttributeCollection attCol;

                    bool IsGuid;
                    foreach (System.Xml.XmlNode listItem in root)
                    {
                        string name = listItem.Name;
                        string value = listItem.OuterXml;
                        attCol = listItem.Attributes;

                        for (int i = 0; i < attCol.Count; i++)
                        {
                            IsGuid = attCol[i].Value.ToString().Contains("{");
                            if (IsGuid)
                            {
                                uniqueID = attCol[i].Value.ToString().Split('{');
                                uniqueGuid = uniqueID[1].ToString().Replace("}", "");
                                //str1.Add(uniqueGuid);
                                break;
                            }


                        }
                        if (!string.IsNullOrEmpty(uniqueGuid))
                        {
                            break;
                        }

                    }
        #endregion


    }

            catch (Exception exc)
            {

                throw ;
            }
    Guid currentItemGuid = new Guid(uniqueGuid);
    return currentItemGuid;
    }
}

您需要使用有效的凭据。 由于共享点认识用户,因此DefaultCredentials在您的计算机上工作。 如果共享点在另一台计算机上,则需要使用在SharePoint服务器上有效的用户名和密码来创建NetwordCredentials。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM