2012-03-30 32 views
0

このMSDNチュートリアルに従ってWebレスポンスを取得しようとしましたが、レスポンスが戻ってこないので、Web要求を送信するための既定またはネットワーク資格情報以外のものを使用できるかどうか疑問です。System.Net.WebRequestカスタム資格情報

私は、Sharepointのカスタムタイマージョブがフィーチャーレシーバーを使用してインストールして、それは、ここでのコードだexecuteメソッド

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で、管理者として記録されています。資格コードラインをコメントアウトしたり、ネットワーク資格情報を試してみましたが、動作しないようです。

私はコンソールアプリでコードをテストしようとすると、ああ、それは資格証明書のプロパティがtrue =暗号

乾杯以外NULLであると言います!

答えて

1

msdnからCredentialsのタイプはICredentialsですので、実装が必要です。

NetworkCredentialsオブジェクトまたはCredentialCacheを使用する必要があります。