यदि आप ऐसी फ़ाइल डाउनलोड करने का प्रयास कर रहे हैं जिसके लिए प्रमाणीकरण की आवश्यकता है (जैसे कि टीएफएस वेब, या किसी डोमेन से जुड़े आईआईएस सर्वर), न तो एमएसबिल्ड एक्सटेंशन पैक और न ही एमएसबिल्ड कम्युनिटी टास्क में एक देने की क्षमता है HTTP सर्वर पर उपयोगकर्ता नाम या पासवर्ड। इस मामले में, मैंने कस्टम एमएसबिल्ड कार्य लिखना समाप्त कर दिया। मैंने जो किया है वह यहाँ है।
मैंने Download a file which requires authentication using vb.net/c#? के उत्तर में स्टैक ओवरफ़्लो उपयोगकर्ता डौग की सलाह का पालन किया, जिसमें उन्होंने कोड गुरु वेब साइट पर टॉम आर्चर द्वारा लिखी गई विधि में जोड़ने के लिए कुछ कोड सुझाए।
तो मैं निम्न कोड एक MSBuild लक्ष्य Wget नामित बनाने के लिए के साथ एक नई सी # प्रोजेक्ट बनाने के लिए एमएस दृश्य स्टूडियो 2010 का इस्तेमाल किया (पूर्ण स्रोत कोड दिखाया गया है):
// Include references to the following frameworks in your solution:
// - Microsoft.Build.Framework
// - Microsoft.Build.Utilities.v4.0
// - System
// - System.Net
using System;
using System.Net;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Wget
{
public class Wget: Task
{
[Required]
public String Address // HTTP address to access
{ get; set; }
[Required]
public String LocalFilename // Local file to which the downloaded page will be saved
{ get; set; }
public String Username // Credential for HTTP authentication
{ get; set; }
public String Password // Credential for HTTP authentication
{ get; set; }
public override bool Execute()
{
int read = DownloadFile(Address, LocalFilename, Username, Password);
Console.WriteLine("{0} bytes written", read);
return true;
}
public static int DownloadFile(String remoteFilename, String localFilename, String httpUsername, String httpPassword)
{
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;
// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
// If a username or password have been given, use them
if (httpUsername.Length > 0 || httpPassword.Length > 0)
{
string username = httpUsername;
string password = httpPassword;
request.Credentials = new System.Net.NetworkCredential(username, password);
}
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
// Return total bytes processed to caller.
return bytesProcessed;
}
}
}
उस जगह में साथ
, मैं जोड़ सकते हैं मेरी MSBuild परियोजना के लिए निम्न कार्य:
<!-- Get the contents of a Url-->
<Wget
Address="http://mywebserver.com/securepage"
LocalFilename="mydownloadedfile.html"
Username="myusername"
Password="mypassword">
</Wget>
Wget कार्य पेज mywebserver.com द्वारा सेवा डाउनलोड करता है और mydownloadedfile.html के रूप में वर्तमान कार्यशील निर्देशिका में एक फ़ाइल में बचत होती है, उपयोगकर्ता नाम "myusername" और पासवर्ड का उपयोग "मेरा पासवर्ड"।
हालांकि, कस्टम Wget MSBuild कार्य का उपयोग करने के लिए, मुझे MSBuild को Wget असेंबली फ़ाइल (.dll) कहां मिलनी चाहिए। इस MSBuild के तत्व के साथ किया जाता है:
<!-- Import your custom MSBuild task -->
<UsingTask AssemblyFile="MyCustomMSBuildTasks\Wget\bin\Release\Wget.dll" TaskName="Wget" />
आप कल्पना प्राप्त करना चाहते हैं, तो आप भी अपने MSBuild परियोजना Wget निर्माण से पहले यह कहा जाता है हो सकता है। ऐसा करने के लिए, <MSBuild Projects>
कार्य के साथ समाधान का निर्माण, और <UsingTaks AssemblyFile>
कार्य के साथ यह आयात करते हैं, कुछ इस तरह:
<!-- Build the custom MSBuild target solution-->
<MSBuild Projects="MyCustomMSBuildTasks\CustomBuildTasks.sln" Properties="Configuration=Release" />
<!-- Import your custom MSBuild task -->
<UsingTask AssemblyFile="MyCustomMSBuildTasks\Wget\bin\Release\Wget.dll" TaskName="Wget" />
<!-- Get the contents of a Url-->
<Wget
Address="http://mywebserver.com/securepage"
LocalFilename="mydownloadedfile.html"
Username="myusername"
Password="mypassword">
</Wget>
तो आप एक कस्टम MSBuild लक्ष्य पहले कभी नहीं बनाया गया है, यह भी मुश्किल नहीं है - एक बार आप मूल बातें जानते हैं।ऊपर सी # कोड देखें, आधिकारिक एमएसडीएन दस्तावेज पर नज़र डालें, और अधिक उदाहरणों के लिए वेब पर चारों ओर खोजें। एक अच्छी जगह शुरू करने के लिए है:
समुदायटास्क का वेबडाउनलोड अब प्रमाणीकरण का समर्थन करता है। – rasjani