Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 68
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Simple Web-Request with Web-Response


Autor: Linoge
Sprache: C#
Bewertung:
7.15 (3 votes)
Anzahl der Aufrufe: 15315
  
Kick it on dotnet-kicks.de  

Beschreibung:

Sample to build a simple Web-Request with a simple Server-Web-Response interpretation

Abgelegt unter: HttpWebResponse, HttpWebRequest, WebRequest, WebResponse, Web, Http.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Create a Web-Request to an URL
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com/en/us/default.aspx");

//Defined poperties for the Web-Request
httpWebRequest.Method = "POST";
httpWebRequest.MediaType = "HTTP/1.1";
httpWebRequest.ContentType = "text/xml";
httpWebRequest.UserAgent = "Example Client";

//Defined data for the Web-Request
byte[] byteArrayData = Encoding.ASCII.GetBytes("A string you would like to send");
httpWebRequest.ContentLength = byteArrayData.Length;

//Attach data to the Web-Request
Stream dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArrayData, 0, byteArrayData.Length);
dataStream.Close();

//Send Web-Request and receive a Web-Response
HttpWebResponse httpWebesponse = (HttpWebResponse)httpWebRequest.GetResponse();

//Translate data from the Web-Response to a string
dataStream = httpWebesponse.GetResponseStream();
StreamReader streamreader = new StreamReader(dataStream, Encoding.UTF8);
string response = streamreader.ReadToEnd();
streamreader.Close();
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Timo Boehme schrieb am:  03.03.2008 15:00:50

Where is SR_DataStream declared? Wo ist SR_DataStream deklariert?
Linoge schrieb am:  03.03.2008 15:35:36

Ich habe den Snippet verbessert.
Christian R. schrieb am:  08.08.2010 23:59:19

I understand the "Defined data for the Web-Request" as the possibility to send GET or POST parameters. But a var_dump() of the $_SERVER on my Apache doesn't show me the sent data. Might I understand the data stream wrong or there is something wrong with the code?
Christian R. schrieb am:  09.08.2010 11:09:28

Problem solved. The dataStream is appended as raw post data an can be read server side with $HTTP_RAW_POST_DATA.


Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.