using System;using System.IO;using System.Net;using System.Text;
namespace Examples.System.Net{public class WebRequestPostExample{public static void Main(){// Create a request using a URL that can receive a post.WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx");// Set the Method property of the request to POST.request.Method = "POST";// Create POST data and convert it to a byte array.string postData = "This is a test that posts this string to a Web server.";byte[] byteArray = Encoding.UTF8.GetBytes(postData);// Set the ContentType property of the WebRequest.request.ContentType = "application/x-www-form-urlencoded";// Set the ContentLength property of the WebRequest.request.ContentLength = byteArray.Length;// Get the request stream.Stream dataStream = request.GetRequestStream();// Write the data to the request stream.dataStream.Write(byteArray, 0, byteArray.Length);// Close the Stream object.dataStream.Close();// Get the response.WebResponse response = request.GetResponse();// Display the status.Console.WriteLine(((HttpWebResponse)response).StatusDescription);// Get the stream containing content returned by the server.dataStream = response.GetResponseStream();// Open the stream using a StreamReader for easy access.StreamReader reader = new StreamReader(dataStream);// Read the content.string responseFromServer = reader.ReadToEnd();// Display the content.Console.WriteLine(responseFromServer);// Clean up the streams.reader.Close();dataStream.Close();response.Close();}}}
var values = new Dictionary<string, string>\{\{ "thing1", "hello" },{ "thing2", "world" }};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
获取
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
var client = new RestClient("http://example.com");// client.Authenticator = new HttpBasicAuthenticator(username, password);var request = new RestRequest("resource/{id}");request.AddParameter("thing1", "Hello");request.AddParameter("thing2", "world");request.AddHeader("header", "value");request.AddFile("file", path);var response = client.Post(request);var content = response.Content; // Raw content as stringvar response2 = client.Post<Person>(request);var name = response2.Data.Name;
var responseString = await "http://www.example.com/recepticle.aspx".GetStringAsync();
方法C:HttpWebRequest(不推荐用于新工作)
可用于:. NET Framework 1.1+、. NET Standard 2.0+、. NET Core 1.0+。在. NET Core中,它主要是为了兼容性-它包装了HttpClient,性能较差,并且不会获得新功能。
using System.Net;using System.Text; // For class Encodingusing System.IO; // For StreamReader
发布
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=" + Uri.EscapeDataString("hello");postData += "&thing2=" + Uri.EscapeDataString("world");var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";request.ContentLength = data.Length;
using (var stream = request.GetRequestStream()){stream.Write(data, 0, data.Length);}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
获取
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
可用于:. NET Framework 1.1+、NET Standard 2.0+和. NET Core 2.0+。
在某些情况下(. NET Framework 4.5-4.8),如果您需要同步执行HTTP请求,仍然可以使用WebClient。
using System.Net;using System.Collections.Specialized;
发布
using (var client = new WebClient()){var values = new NameValueCollection();values["thing1"] = "hello";values["thing2"] = "world";
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);}
获取
using (var client = new WebClient()){var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");}
using System.Net;
...
using (var wb = new WebClient()){var response = wb.DownloadString(url);}
简单的POST请求
using System.Net;using System.Collections.Specialized;
...
using (var wb = new WebClient()){var data = new NameValueCollection();data["username"] = "myUser";data["password"] = "myPassword";
var response = wb.UploadValues(url, "POST", data);string responseInString = Encoding.UTF8.GetString(response);}
var client = new WebClient();string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}";//If you have your data stored in an object serialize it into json to pass to the webclient with Newtonsoft's JsonConvertvar encodedJson = JsonConvert.SerializeObject(newAccount);
client.Headers.Add($"x-api-key:{ApiKey}");client.Headers.Add("Content-Type:application/json");try{var response = client.UploadString($"{apiurl}", encodedJson);//if you have a model to deserialize the json into Newtonsoft will help bind the data to the model, this is an extremely useful trick for GET calls when you have a lot of data, you can strongly type a model and dump it into an instance of that class.Response response1 = JsonConvert.DeserializeObject<Response>(response);
var client = new TinyRestClient(new HttpClient(), "http://MyAPI.com/api");// POSTvar city = new City() { Name = "Paris", Country = "France" };// With contentvar response = await client.PostRequest("City", city).ExecuteAsync<bool>();
这是我在. NET 4.8中使用的HTTP POST请求。使用此代码,可以一次发送多个POST请求异步。
在每个请求结束时都会引发一个事件。并且在所有请求结束时都会引发另一个事件。
下面是核心类:
Imports System.ComponentModelImports System.Text.RegularExpressionsImports System.TimersImports System.Windows.FormsImports AeonLabsImports AeonLabs.EnvironmentImports Newtonsoft.Json
Public Class HttpDataCorePublic Property url As StringPublic Property state As New environmentVarsCorePublic Property errorMessage As String = ""Public Property statusMessage As StringPublic Property threadCount As Integer = 25Public Property numberOfRetryAttempts = 5Public Property queue As List(Of _queue_data_struct)Public Property queueBWorker As Integer() ' has the size of threadCountPublic Property queueLock As New ObjectPublic Property retryAttempts As New _retry_attemptsPublic Property dataStatistics As List(Of _data_statistics)Public Property loadingCounter As IntegerPublic Property CompletionPercentage As Integer ' value range 0-100Public Property IsBusy As Boolean
Public Structure _queue_data_structDim vars As Dictionary(Of String, String)Dim filenameOrSavePath As String ' full address file name or full adress folder pathDim misc As Dictionary(Of String, String)Dim status As Integer ' -1 - completed; 0- not sent yet; 1-already sent / processingEnd Structure
Public Structure _retry_attemptsDim counter As IntegerDim pattern As IntegerDim previousPattern As IntegerDim errorMessage As StringEnd Structure
Public Structure _data_statisticsDim filesize As DoubleDim bytesSentReceived As DoubleDim speed As DoubleEnd Structure
Public WithEvents RestartQueueTimer As New Timers.TimerPublic bwDataRequest() As BackgroundWorker
Public Event requestCompleted(sender As Object, requestData As String) 'TODO add misc vars
Private sendToQueue As Boolean
Public Sub New(ByVal Optional _state As environmentVarsCore = Nothing, ByVal Optional _url As String = "")queue = New List(Of _queue_data_struct)dataStatistics = New List(Of _data_statistics)loadingCounter = 0sendToQueue = FalseIf _state IsNot Nothing AndAlso _url.Equals("") Thenurl = _state.ServerBaseAddr & _state.ApiServerAddrPathElseIf Not _url.Equals("") Thenurl = _urlElseThrow New System.Exception("Initialization err: state and url cannot be both null at same time")End If
If _state IsNot Nothing Thenstate = _stateEnd IfEnd Sub
Public Sub loadQueue(ByVal vars As Dictionary(Of String, String), ByVal Optional misc As Dictionary(Of String, String) = Nothing, ByVal Optional filenameOrSavePath As String = Nothing)Dim queueItem As New _queue_data_structqueueItem.vars = New Dictionary(Of String, String)queueItem.misc = New Dictionary(Of String, String)
queueItem.vars = varsqueueItem.status = 0queueItem.misc = miscqueueItem.filenameOrSavePath = filenameOrSavePathqueue.Add(queueItem)End Sub
Public Sub clearQueue()loadingCounter = 0queue = New List(Of _queue_data_struct)End Sub
Public Sub startRequest()If bwDataRequest(0) Is Nothing ThenThrow New Exception("You need to call initialze first")Exit SubEnd If
'startSendQueue()IsBusy = True
AddHandler RestartQueueTimer.Elapsed, New ElapsedEventHandler(AddressOf QueueTimerTick)With RestartQueueTimer.Enabled = True.Interval = 500.Start()End WithEnd Sub
Private Sub QueueTimerTick(ByVal sender As Object, ByVal e As ElapsedEventArgs)If QueuesToComplete(queue).Equals(0) And QueuesToSend(queue).Equals(0) ThenRestartQueueTimer.Stop()queue = New List(Of _queue_data_struct)RaiseEvent requestCompleted(Me, Nothing)IsBusy = FalseExit SubEnd If
If retryAttempts.counter >= numberOfRetryAttempts Then 'ToDo a retry number of attempts before quitsThreading.Thread.CurrentThread.CurrentUICulture = Globalization.CultureInfo.GetCultureInfo(state.currentLang)Dim MsgBox As messageBoxFormMsgBox = New messageBoxForm(retryAttempts.errorMessage & ". " & My.Resources.strings.tryAgain & " ?", My.Resources.strings.question, MessageBoxButtons.YesNo, MessageBoxIcon.Question)If MsgBox.ShowDialog() = DialogResult.Yes ThenDim retry As _retry_attemptsWith retry.counter = 0.previousPattern = -1.pattern = 0.errorMessage = ""End WithretryAttempts = retrystartSendQueue()ElseRestartQueueTimer.Stop()queue = New List(Of _queue_data_struct)RaiseEvent requestCompleted(Me, Nothing)IsBusy = FalseExit SubEnd IfExit SubElseIf Not sendToQueue And QueuesToSend(queue) > 0 ThenstartSendQueue()End IfEnd Sub
Private Sub startSendQueue()sendToQueue = TrueWhile QueuesToSend(queue) > 0For shtIndex = 0 To threadCountFor i = 0 To queue.Count - 1If Not bwDataRequest(shtIndex).IsBusy ThenSyncLock queueLockIf queue.ElementAt(i).status.Equals(0) ThenDim data As New _queue_data_structdata.vars = queue.ElementAt(i).varsdata.status = 1data.misc = queue.ElementAt(i).miscdata.filenameOrSavePath = queue.ElementAt(i).filenameOrSavePathqueue(i) = dataqueueBWorker(shtIndex) = idataStatistics(shtIndex) = (New _data_statistics)
bwDataRequest(shtIndex).RunWorkerAsync(queue(i))Threading.Thread.Sleep(50)End IfEnd SyncLockEnd IfNext iNext shtIndexEnd WhilesendToQueue = FalseEnd Sub
Public Function QueuesToSend(queue As List(Of _queue_data_struct)) As IntegerDim counter As Integer = 0For i = 0 To queue.Count - 1If queue(i).status.Equals(0) Thencounter += 1End IfNext iReturn counterEnd Function
Public Function QueuesToComplete(queue As List(Of _queue_data_struct)) As IntegerDim counter As Integer = 0For i = 0 To queue.Count - 1If queue(i).status.Equals(1) Thencounter += 1End IfNext iReturn counterEnd Function
Public Function QueuesMultiHash(queue As List(Of _queue_data_struct)) As IntegerDim counter As Integer = 0For i = 0 To queue.Count - 1If queue(i).status.Equals(1) Thencounter += iEnd IfNext iReturn counterEnd Function
Public Function IsBase64String(ByVal s As String) As Booleans = s.Trim()Return (s.Length Mod 4 = 0) AndAlso Regex.IsMatch(s, "^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None)End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Public Function ConvertDataToArray(key As String, fields As String(), response As String) As Dictionary(Of String, List(Of String))If GetMessage(response).Equals("1001") ThenThreading.Thread.CurrentThread.CurrentUICulture = Globalization.CultureInfo.GetCultureInfo(state.currentLang)errorMessage = "{'error':true,'message':'" & My.Resources.strings.errorNoRecordsFound & "'}"Return NothingEnd IfTryDim jsonResult = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(response)If jsonResult.ContainsKey(key) ThenIf Not jsonResult.Item(key).item(0).Count.Equals(fields.Length) ThenThreading.Thread.CurrentThread.CurrentUICulture = Globalization.CultureInfo.GetCultureInfo(state.currentLang)errorMessage = "{'error':true,'message':'" & My.Resources.strings.JsonFieldsMismatch & ". table(" & key & "'}"Return NothingElseDim results = New Dictionary(Of String, List(Of String))For k = 0 To fields.Length - 1Dim fieldValues As List(Of String) = New List(Of String)For i = 0 To jsonResult.Item(key).Count - 1fieldValues.Add(jsonResult.Item(key).item(i).item(k).ToString)Next iresults.Add(fields(k), fieldValues)
Next kReturn resultsEnd IfElseThreading.Thread.CurrentThread.CurrentUICulture = Globalization.CultureInfo.GetCultureInfo(state.currentLang)errorMessage = "{'error':true,'message':'" & My.Resources.strings.JsonkeyNotFound & " (" & key & "'}"Return NothingEnd IfCatch ex As ExceptionerrorMessage = "{'error':true,'message':'" & ex.ToString & "'}"errorMessage = ex.ToStringReturn NothingEnd TryEnd FunctionEnd Class
AeonLabs.Envoriment是一个具有集合或字段和属性的类。
下面的一个是用于发出POST请求:
Imports System.ComponentModelImports System.IOImports System.NetImports System.TextImports System.WebImports System.Web.Script.SerializationImports System.Windows.FormsImports AeonLabs.EnvironmentImports AeonLabs.Security
Public Class HttpDataPostDataInherits HttpDataCore
Public Event updateProgress(sender As Object, misc As Dictionary(Of String, String))
Public Event dataArrived(sender As Object, requestData As String, misc As Dictionary(Of String, String))
Public Sub New(ByVal Optional _state As environmentVarsCore = Nothing, ByVal Optional _url As String = "")MyBase.New(_state, _url)End Sub
Public Sub initialize(ByVal Optional _threadCount As Integer = 0)If Not _threadCount.Equals(0) ThenthreadCount = _threadCountEnd If
ReDim bwDataRequest(threadCount)ReDim queueBWorker(threadCount)
For shtIndex = 0 To threadCountdataStatistics.Add(New _data_statistics)
bwDataRequest(shtIndex) = New System.ComponentModel.BackgroundWorkerbwDataRequest(shtIndex).WorkerReportsProgress = TruebwDataRequest(shtIndex).WorkerSupportsCancellation = True
AddHandler bwDataRequest(shtIndex).DoWork, AddressOf bwDataRequest_DoWorkAddHandler bwDataRequest(shtIndex).RunWorkerCompleted, AddressOf bwDataRequest_RunWorkerCompletedNext shtIndexDim retry As _retry_attemptsWith retry.counter = 0.previousPattern = -1.pattern = 0.errorMessage = ""End WithretryAttempts = retryEnd Sub
Private Sub bwDataRequest_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
' Find out the Index of the bWorker that called this DoWork (could be cleaner, I know)Dim Y As IntegerDim Index As Integer = NothingFor Y = 0 To UBound(bwDataRequest)If sender.Equals(bwDataRequest(Y)) ThenIndex = YExit ForEnd IfNext Y
Dim queue As _queue_data_structqueue = e.Argument
Dim vars As New Dictionary(Of String, String)vars = queue.vars
'TODO translation need to be localIf Not System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() ThenThreading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(state.currentLang)e.Result = "{'error':true,'message':'" & My.Resources.strings.errorNoNetwork & "'}"Exit SubEnd IfIf vars Is Nothing Thene.Result = "{'error':true,'message':'missconfiguration vars'}"Exit SubEnd If
If Not vars.ContainsKey("id") Thenvars.Add("id", state.userId)End IfIf Not vars.ContainsKey("pid") ThenDim appId As New FingerPrintvars.Add("pid", appId.Value)End IfIf Not vars.ContainsKey("language") Thenvars.Add("language", state.currentLang)End IfIf Not vars.ContainsKey("origin") Thenvars.Add("origin", state.softwareAccessMode)End If
Dim serializer As New JavaScriptSerializer()Dim json As String = serializer.Serialize(vars)Dim encryption As New AesCipher(state)Dim encrypted As String = HttpUtility.UrlEncode(encryption.encrypt(json))Dim PostData = "origin=" & state.softwareAccessMode & "&data=" & encryptedDim request As WebRequest = WebRequest.Create(url)Dim responseFromServer As String = ""Dim decrypted As String = ""
request.Method = "POST"Dim byteArray As Byte() = Encoding.UTF8.GetBytes(PostData)request.ContentType = "application/x-www-form-urlencoded"request.Headers.Add("Authorization", state.ApiHttpHeaderToken & "-" & state.softwareAccessMode)request.ContentLength = byteArray.LengthTryDim dataStream As Stream = request.GetRequestStream()dataStream.Write(byteArray, 0, byteArray.Length)dataStream.Close()Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)dataStream = response.GetResponseStream()Dim reader As New StreamReader(dataStream)responseFromServer = reader.ReadToEnd()reader.Close()dataStream.Close()response.Close()
If response.StatusCode = HttpStatusCode.Accepted Or response.StatusCode = 200 ThenIf IsBase64String(responseFromServer) And Not responseFromServer.Equals("") Thendecrypted = encryption.decrypt((responseFromServer)).Replace("\'", "'")ElseThreading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(state.currentLang)decrypted = "{'error':true,'encrypted':false,'message':'" & My.Resources.strings.contactingCommServer & " |" & responseFromServer & "|'}"End IfElseThreading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(state.currentLang)decrypted = "{'error':true,'message':'" & My.Resources.strings.contactingCommServer & " (" & response.StatusCode & ")', 'statuscode':'" & response.StatusCode & "'}"End IfCatch ex As ExceptionThreading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(state.currentLang)decrypted = "{'error':true,'message':'" & My.Resources.strings.contactingCommServer & " (" & ex.Message.ToString.Replace("'", "\'") & ")'}"End Try
e.Result = decrypted.Replace("\'", "'")End Sub
Private Sub bwDataRequest_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)' Find out the Index of the bWorker that called this DoWork (could be cleaner, I know)Dim Y As IntegerDim Index As Integer = NothingDim data As New _queue_data_struct
For Y = 0 To UBound(bwDataRequest)If sender.Equals(bwDataRequest(Y)) ThenIndex = YExit ForEnd IfNext Y
If IsResponseOk(e.Result, "statuscode") Thendata = New _queue_data_structdata = queue(queueBWorker(Index))data.status = 0 're queue the fileSyncLock queueLockqueue(queueBWorker(Index)) = dataEnd SyncLockDim errorMsg As String = GetMessage(e.Result)Dim retry As _retry_attemptsWith retry.counter = retryAttempts.counter.previousPattern = retryAttempts.previousPattern.pattern = retryAttempts.pattern.errorMessage = retryAttempts.errorMessageEnd Withretry.errorMessage = If(retryAttempts.errorMessage.IndexOf(errorMsg) > -1, retryAttempts.errorMessage, retryAttempts.errorMessage & System.Environment.NewLine & errorMsg)
retry.pattern = QueuesMultiHash(queue)If retry.previousPattern.Equals(retry.pattern) Thenretry.counter += 1Elseretry.counter = 1retry.previousPattern = retryAttempts.patternEnd If
retryAttempts = retryExit SubEnd If
data = New _queue_data_structdata = queue(queueBWorker(Index))data.status = -1 'completed sucessfully statusSyncLock queueLockqueue(queueBWorker(Index)) = dataEnd SyncLock
loadingCounter += 1CompletionPercentage = (loadingCounter / queue.Count) * 100statusMessage = "Loading data from the cloud..."RaiseEvent updateProgress(Me, queue(queueBWorker(Index)).misc)RaiseEvent dataArrived(Me, e.Result, queue(queueBWorker(Index)).misc)End SubEnd Class