英語で読む

次の方法で共有


HttpWebRequest.Referer プロパティ

定義

Referer HTTP ヘッダーの値を取得または設定します。

public string? Referer { get; set; }
public string Referer { get; set; }

プロパティ値

Referer HTTP ヘッダーの値。 既定値は nullです。

次のコード例では、Referer プロパティを設定します。

// Create a 'HttpWebRequest' object.
HttpWebRequest	myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);
// Set referer property  to http://www.microsoft.com .
myHttpWebRequest.Referer="http://www.microsoft.com";
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
// Display the contents of the page to the console.
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuffer = new Char[256];
int count = streamRead.Read( readBuffer, 0, 256 );
Console.WriteLine("\nThe contents of HTML page are.......");	
while (count > 0)
{
    String outputData = new String(readBuffer, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuffer, 0, 256);
}
Console.WriteLine("\nHTTP Request  Headers :\n\n{0}",myHttpWebRequest.Headers);
Console.WriteLine("\nHTTP Response Headers :\n\n{0}",myHttpWebResponse.Headers);	
streamRead.Close();
streamResponse.Close();
// Release the response object resources.
myHttpWebResponse.Close();
Console.WriteLine("Referer to the site is:{0}",myHttpWebRequest.Referer);

注釈

注意事項

WebRequestHttpWebRequestServicePointWebClient は廃止されており、新しい開発には使用しないでください。 代わりに HttpClient を使用してください。

AllowAutoRedirect プロパティが trueされている場合、要求が別のサイトにリダイレクトされるときに、Referer プロパティが自動的に設定されます。

Referer HTTP ヘッダーをクリアするには、Referer プロパティを nullに設定します。

注意

このプロパティの値は、WebHeaderCollectionに格納されます。 WebHeaderCollection が設定されている場合、プロパティ値は失われます。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1