System.Net.WebRequest自定义凭据

时间:2012-03-30 16:03:05

标签: c# .net sharepoint httpwebresponse

尝试按照此MSDN教程获取Web响应但未收到任何响应,因此想知道我是否可以使用除默认或网络凭据之外的任何其他内容来发送Web请求。

我在使用功能接收器安装的Sharepoint自定义计时器作业中使用它,这是代码,

带有执行方法的计时器作业类

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;

namespace EmailJob.FeatureCode
{
    class SharePointWarmupJob : SPJobDefinition
    {
        private const string JOB_NAME = "Email Job";

        public SharePointWarmupJob() : base() { }

        public SharePointWarmupJob(SPWebApplication webApp)
            : base(JOB_NAME, webApp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = JOB_NAME;
        }

        public override void Execute(Guid targetInstanceId)
        {
            Debug.Assert(false);

            if (this.WebApplication.Sites.Count > 0)
                WarmUpSharePointSite(this.WebApplication.Sites[0]);
        }

        private void WarmUpSharePointSite(SPSite siteCollection)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create(siteCollection.Url);
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            System.Net.WebResponse response = request.GetResponse();
            response.Close();
        }
    }
}

功能接收器类

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using EmailJob.FeatureCode;

namespace EmailJob
{
    class EmailJobFeature : SPFeatureReceiver
    {
        private const string JOB_NAME = "Email Job";

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            throw new NotImplementedException();
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            throw new NotImplementedException();
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp == null)
                throw new NotImplementedException("Error obtaining reference to Web application");

            foreach (SPJobDefinition job in webApp.JobDefinitions)
                if (job.Name == JOB_NAME) job.Delete();

            SharePointWarmupJob warmupJob = new SharePointWarmupJob(webApp);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;

            warmupJob.Schedule = schedule;

            warmupJob.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp == null)
                throw new NotImplementedException("Error obtaining reference to Web application");

            foreach (SPJobDefinition job in webApp.JobDefinitions)
                if (job.Name == JOB_NAME) job.Delete();

            throw new NotImplementedException();
        }
    }
}

当我尝试调试时,它在代码行中没有给出任何响应

"System.Net.WebResponse response = request.GetResponse();"

这是我的VPC并以管理员身份登录,我甚至注释掉凭证代码行或尝试过网络凭据,但它似乎无法正常工作。

哦,是的,当我尝试在Console应用程序中测试代码时,它说凭证属性为null,除了encrypt = true

干杯!

1 个答案:

答案 0 :(得分:1)

msdn开始,凭据的类型为ICredentials,因此您需要实施。

幸运的是,他们还声明您应该使用NetworkCredentials对象或CredentialCache;)