Reputation: 2010
Trying to follow this MSDN tutorial to get a web response but not getting any response back, so wondering if I can use anything else except default or network credentials for sending a web request.
I am using it in a Sharepoint Custom Timer Job installed using a Feature Receiver, here's the code,
Timer Job class with execute method
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();
}
}
}
Feature Receiver class
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();
}
}
}
When I try to debug, it gives no response back at code line
"System.Net.WebResponse response = request.GetResponse();"
This is my VPC and am logged as Administrator, I even commented out credentials code line or tried network credentials but it just doesn't seems to work.
Oh yes when i try to test code in Console app, it says the credentials properties are null except encrypt = true
Cheers !
Upvotes: 0
Views: 2377
Reputation: 16618
From msdn, Credentials is of type ICredentials
so you need an implementation.
Fortunately, they also state you should use a NetworkCredentials object or CredentialCache ;)
Upvotes: 1