2012-03-30 5 views
0

Wenn Sie diesem MSDN-Lernprogramm folgen, um eine Webantwort zu erhalten, aber keine Antwort erhalten, fragen Sie sich, ob ich außer Standard- oder Netzwerkanmeldeinformationen zum Senden einer Webanforderung verwenden kann.System.Net.WebRequest Benutzerdefinierte Anmeldeinformationen

ich es in einem Sharepoint Custom Timer Job ist mit installierten Funktionen und Features Receiver verwenden, hier ist der Code,

Timer Job-Klasse mit Methode 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(); 
     } 
    } 
} 

Eigenschaft Empfängerklasse

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(); 
     } 
    } 
} 

Wenn ich versuche, zu debuggen, gibt es keine Antwort zurück an Codezeile

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

Dies ist meine VPC und bin als Administrator angemeldet, ich habe sogar die Anmeldungs-Codezeile auskommentiert oder versuchte Netzwerk-Anmeldeinformationen, aber es scheint einfach nicht zu funktionieren.

Oh ja, wenn ich versuche Code in Console App zu testen, sagt es die Anmeldeinformationen Eigenschaften außer encrypt null sind = true

Prost!

Antwort

1

Von msdn, Credentials ist vom Typ ICredentials, so dass Sie eine Implementierung benötigen.

Glücklicherweise sie erklären Sie auch ein NetworkCredentials Objekt oder CredentialCache verwenden sollte;)