本文整理汇总了C#中WebRequest.GetRequestStream方法的典型用法代码示例。如果您正苦于以下问题:C# WebRequest.GetRequestStream方法的具体用法?C# WebRequest.GetRequestStream怎么用?C# WebRequest.GetRequestStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebRequest
的用法示例。
在下文中一共展示了WebRequest.GetRequestStream方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHttpWebResponse
private static string GetHttpWebResponse(WebRequest httpWebRequest, string postData)
{
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
}
finally
{
requestWriter.Close();
}
return GetHttpWebResponse(httpWebRequest);
}
示例2: SendPostContents
/// <summary>
/// Sends the body text up to the server
/// </summary>
/// <param name="request"></param>
/// <param name="bodyText"></param>
protected static void SendPostContents(WebRequest request, string bodyText)
{
request.Method = "POST";
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
byte[] byteArray = Encoding.UTF8.GetBytes(bodyText);
request.ContentLength = byteArray.Length;
// Get the request stream.
var dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
}
示例3: GetRequestStreamAsync
public override Task<Stream> GetRequestStreamAsync(WebRequest request) => Task.Run(() => request.GetRequestStream());
示例4: SendHttpRequestContents_Inner
/// <summary>
/// Sends the body text up to the server
/// </summary>
/// <param name="request"></param>
/// <param name="bodyText"></param>
private static void SendHttpRequestContents_Inner(WebRequest request, string bodyText, string httpMethod = "POST")
{
request.Method = httpMethod;
// Set the ContentType property of the WebRequest.
request.ContentType = "application/xml;charset=utf-8"; //[2016-06-17] We want to be very explicit on the content type; to give servers very clear instructions on how to parse
// Set the ContentLength property of the WebRequest.
byte[] byteArray = Encoding.UTF8.GetBytes(bodyText);
request.ContentLength = byteArray.Length;
// Get the request stream.
var dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
}
示例5: Ping
public bool Ping(string osVersion, string cjVersion, string machineID)
{
requestPingAborting = false;
// Create a request using a URL that can receive a post.
requestPing = WebRequest.Create (serverUrl + "/ping");
// Set the Method property of the request to POST.
requestPing.Method = "POST";
// Set the ContentType property of the WebRequest.
requestPing.ContentType = "application/json";
// Creates the json object
JsonObject json = new JsonObject();
json.Add("os_version", osVersion);
json.Add("cj_version", cjVersion);
json.Add("machine_id", machineID);
// Converts it to a String
String js = json.ToString();
// Writes the json object into the request dataStream
Stream dataStream;
try {
dataStream = requestPing.GetRequestStream ();
} catch {
this.ResultMessage =
string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
serverUrl);
return false;
}
if(requestPingAborting) {
LogB.Information("Aborted from PingAbort");
return false;
}
dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
dataStream.Close ();
// Get the response.
WebResponse response;
try {
response = requestPing.GetResponse ();
} catch {
this.ResultMessage =
string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
serverUrl);
return false;
}
if(requestPingAborting) {
LogB.Information("Aborted from PingAbort");
return false;
}
// Display the status (will be 201, CREATED)
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Clean up the streams.
dataStream.Close ();
response.Close ();
this.ResultMessage = "Ping sent.";
return true;
}