WebRequest.Create Metoda

Definicja

Inicjuje nowy WebRequestelement .

Przeciążenia

Create(String)
Przestarzałe.

Inicjuje nowe WebRequest wystąpienie dla określonego schematu identyfikatora URI.

Create(Uri)
Przestarzałe.

Inicjuje nowe WebRequest wystąpienie dla określonego schematu identyfikatora URI.

Create(String)

Źródło:
WebRequest.cs
Źródło:
WebRequest.cs
Źródło:
WebRequest.cs

Przestroga

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.

Inicjuje nowe WebRequest wystąpienie dla określonego schematu identyfikatora URI.

public:
 static System::Net::WebRequest ^ Create(System::String ^ requestUriString);
public static System.Net.WebRequest Create (string requestUriString);
[System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Net.WebRequest Create (string requestUriString);
static member Create : string -> System.Net.WebRequest
[<System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Create : string -> System.Net.WebRequest
Public Shared Function Create (requestUriString As String) As WebRequest

Parametry

requestUriString
String

Identyfikator URI identyfikujący zasób internetowy.

Zwraca

Element WebRequest podrzędny dla określonego schematu identyfikatora URI.

Atrybuty

Wyjątki

Schemat żądania określony w elemecie requestUriString nie został zarejestrowany.

requestUriString to null.

Obiekt wywołujący nie ma WebPermissionAttribute uprawnień do nawiązywania połączenia z żądanym identyfikatorem URI lub identyfikatorem URI, do którego jest przekierowywane żądanie.

Identyfikator URI określony w pliku requestUriString nie jest prawidłowym identyfikatorem URI.

Uwaga: na platformie .NET dla aplikacji ze Sklepu Windows lub przenośnej biblioteki klas przechwyć wyjątek klasy bazowej , FormatExceptionzamiast tego.

Przykłady

W poniższym przykładzie użyto Create funkcji do utworzenia HttpWebRequest wystąpienia wystąpienia. Ciąg reprezentujący docelowy adres URL jest używany jako parametr konstruktora.

Uri^ ourUri = gcnew Uri( url );

// Create a 'WebRequest' object with the specified url.
WebRequest^ myWebRequest = WebRequest::Create( url );

// Send the 'WebRequest' and wait for response.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if ( ourUri->Equals( myWebResponse->ResponseUri ) )
{
   Console::WriteLine( "\nRequest Url : {0} was not redirected", url );
}
else
{
   Console::WriteLine( "\nRequest Url : {0} was redirected to {1}", url, myWebResponse->ResponseUri );
}

// Release resources of response object.
myWebResponse->Close();
Uri ourUri = new Uri(url);            

// Create a 'WebRequest' object with the specified url. 
WebRequest myWebRequest = WebRequest.Create(url); 

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if (ourUri.Equals(myWebResponse.ResponseUri))
    Console.WriteLine("\nRequest Url : {0} was not redirected",url);   
else
    Console.WriteLine("\nRequest Url : {0} was redirected to {1}",url,myWebResponse.ResponseUri);   
// Release resources of response object.
myWebResponse.Close();

Dim ourUri As New Uri(url)
' Create a 'WebRequest' object with the specified url. 

Dim myWebRequest As WebRequest = WebRequest.Create(url)

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' "ResponseUri" property is used to get the actual Uri from where the response was attained.
If ourUri.Equals(myWebResponse.ResponseUri) Then
    Console.WriteLine(ControlChars.Cr + "Request Url : {0} was not redirected", url)
Else
    Console.WriteLine(ControlChars.Cr + "Request Url : {0} was redirected to {1}", url, myWebResponse.ResponseUri)
End If 

' Release resources of response object.
myWebResponse.Close()

Uwagi

Metoda Create zwraca element podrzędny WebRequest klasy określonej w czasie wykonywania jako najbliższe zarejestrowane dopasowanie dla requestUriklasy .

Na przykład gdy identyfikator URI rozpoczynający się od http:// lub https:// jest przekazywany w requestUrielemecie , element HttpWebRequest jest zwracany przez .Create Jeśli identyfikator URI rozpoczynający się od ftp:// jest przekazywany, Create metoda zwróci FtpWebRequest wystąpienie. Jeśli identyfikator URI rozpoczynający się od file:// jest przekazywany, Create metoda zwróci FileWebRequest wystąpienie.

Zarejestrowane typy rezerw wstępnie zarejestrowane obejmują następujące elementy:

  • http://

  • https://

  • ftp://

  • file://

Platforma .NET obejmuje obsługę schematów identyfikatorów http://URI , https://, ftp://i file:// . Niestandardowe WebRequest elementy podrzędne do obsługi innych żądań są rejestrowane za pomocą RegisterPrefix metody .

Metoda Create używa parametru , requestUriString aby utworzyć Uri wystąpienie, które przekazuje do nowego WebRequestobiektu .

Uwaga

Ten element członkowski generuje informacje ze śledzenia pod warunkiem włączenia funkcji śledzenia sieci w aplikacji. Aby uzyskać więcej informacji, zobacz Śledzenie sieci w .NET Framework.

Zobacz też

Dotyczy

Create(Uri)

Źródło:
WebRequest.cs
Źródło:
WebRequest.cs
Źródło:
WebRequest.cs

Przestroga

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.

Inicjuje nowe WebRequest wystąpienie dla określonego schematu identyfikatora URI.

public:
 static System::Net::WebRequest ^ Create(Uri ^ requestUri);
public static System.Net.WebRequest Create (Uri requestUri);
[System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Net.WebRequest Create (Uri requestUri);
static member Create : Uri -> System.Net.WebRequest
[<System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Create : Uri -> System.Net.WebRequest
Public Shared Function Create (requestUri As Uri) As WebRequest

Parametry

requestUri
Uri

Identyfikator Uri URI żądanego zasobu.

Zwraca

Element potomny WebRequest dla określonego schematu identyfikatora URI.

Atrybuty

Wyjątki

Schemat żądania określony w pliku requestUri nie jest zarejestrowany.

requestUri to null.

Obiekt wywołujący nie ma WebPermissionAttribute uprawnień do nawiązywania połączenia z żądanym identyfikatorem URI lub identyfikatorem URI, do którego jest przekierowywane żądanie.

Przykłady

W poniższym przykładzie użyto Create funkcji do utworzenia HttpWebRequest wystąpienia wystąpienia. Identyfikator URI reprezentujący docelowy adres URL jest używany jako parametr konstruktora.

// Create a new 'Uri' object with the specified string.
Uri^ myUri = gcnew Uri( "http://www.contoso.com" );
// Create a new request to the above mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( myUri );
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();
// Create a new 'Uri' object with the specified string.
Uri myUri =new Uri("http://www.contoso.com");
// Create a new request to the above mentioned URL.	
WebRequest myWebRequest= WebRequest.Create(myUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse= myWebRequest.GetResponse();
' Create a new 'Uri' object with the specified string.
Dim myUri As New Uri("http://www.contoso.com")
' Create a new request to the above mentioned URL.	
Dim myWebRequest As WebRequest = WebRequest.Create(myUri)
'  Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

Uwagi

Metoda Create zwraca element podrzędny WebRequest klasy określonej w czasie wykonywania jako najbliższe zarejestrowane dopasowanie dla requestUriklasy .

Jeśli na przykład utworzysz element podrzędny WebRequest , program Handler1, do obsługi żądań do http://www.contoso.com/text/ i innego o nazwie Handler2 do obsługi żądań do http://www.contoso.com/code/programu , można użyć Create metody , aby zwrócić element podrzędny WebRequest skojarzony z określonym identyfikatorem URI.

Aby zwrócić element podrzędny WebRequest klasy na podstawie tylko części schematu identyfikatora URI, użyj CreateDefault metody .

Na przykład gdy identyfikator URI rozpoczynający się od http:// lub https:// jest przekazywany w requestUrielemecie , element HttpWebRequest jest zwracany przez .Create Jeśli identyfikator URI rozpoczynający się od ftp:// jest przekazywany, Create metoda zwróci FileWebRequest wystąpienie. Jeśli identyfikator URI rozpoczynający się od file:// jest przekazywany, Create metoda zwróci FileWebRequest wystąpienie.

Zarejestrowane typy rezerw wstępnie zarejestrowane obejmują następujące elementy:

  • http://

  • https://

  • ftp://

  • file://

Platforma .NET obejmuje obsługę schematów identyfikatorów http://URI , https://, ftp://i file:// . Niestandardowe WebRequest elementy podrzędne do obsługi innych żądań są rejestrowane za pomocą RegisterPrefix metody .

Uwaga

Ten element członkowski generuje informacje ze śledzenia pod warunkiem włączenia funkcji śledzenia sieci w aplikacji. Aby uzyskać więcej informacji, zobacz Śledzenie sieci w .NET Framework.

Dotyczy