本文整理汇总了C#中System.Net.HttpWebRequest.GetRequestStream方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.GetRequestStream方法的具体用法?C# HttpWebRequest.GetRequestStream怎么用?C# HttpWebRequest.GetRequestStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpWebRequest
的用法示例。
在下文中一共展示了HttpWebRequest.GetRequestStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertSoapEnvelopeIntoWebRequest
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
示例2: SendRequest
public void SendRequest(HttpWebRequest request)
{
int index = 0, size = 0;
byte[][] data = new byte[Count * 2][];
foreach (string key in AllKeys)
{
data[index] = HttpUtility.UrlEncodeToBytes(key);
size += data[index++].Length;
data[index] = HttpUtility.UrlEncodeToBytes(this[key]);
size += data[index++].Length;
}
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = size + Count * 2 - 1;
using (Stream requestStream = request.GetRequestStream())
{
for (int i = 0; i < data.Length; i++)
{
byte[] buff = data[i];
requestStream.Write(buff, 0, buff.Length);
if (i < data.Length - 1)
requestStream.WriteByte(SeparatorBytes[i % 2]);
}
}
}
示例3: RequestAndRespond
private Result RequestAndRespond(HttpWebRequest request, string content)
{
HttpStatusCode statusCode = HttpStatusCode.NotFound;
try
{
var contentBytes = Encoding.UTF8.GetBytes(content);
request.ContentLength = contentBytes.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(contentBytes, 0, contentBytes.Length);
}
using (var response = request.GetResponse() as HttpWebResponse)
{
statusCode = HttpClient.GetStatusCode(response);
}
}
catch (WebException webException)
{
var response = webException.Response as HttpWebResponse;
statusCode = HttpClient.GetStatusCode(response);
}
return HttpStatusCodeExtensions.ToResult(statusCode);
}
示例4: AddXmlToRequest
/// <summary>
/// Adds the XML to web request. The XML is the standard
/// XML used by RPC-XML requests.
/// </summary>
private static void AddXmlToRequest(Uri sourceUrl, Uri targetUrl, HttpWebRequest webreqPing)
{
Stream stream = (Stream)webreqPing.GetRequestStream();
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII))
{
writer.WriteStartDocument(true);
writer.WriteStartElement("methodCall");
writer.WriteElementString("methodName", "pingback.ping");
writer.WriteStartElement("params");
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", sourceUrl.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", targetUrl.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
示例5: EjecutarAccion
public string EjecutarAccion(string url, string metodo, object modelo = null)
{
request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 10 * 1000;
request.Method = metodo;
request.ContentType = "application/json; charset=utf-8";
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(usuario + ":" + clave));
request.Headers.Add("Authorization", "Basic " + credentials);
if (modelo != null)
{
var postString = new JavaScriptSerializer().Serialize(modelo);
byte[] data = UTF8Encoding.UTF8.GetBytes(postString);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, 0, data.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode != HttpStatusCode.NoContent)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
return "";
}
示例6: Login
public Login(string Username, string Password)
{
string url = string.Format("http://www.myfitnesspal.com/account/login");
request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string NewValue = string.Format("username={0}&password={1}&remember_me=1",Username,Password);
request.ContentLength = NewValue.Length;
// Write the request
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(NewValue);
stOut.Close();
// Do the request to get the response
response = (HttpWebResponse)request.GetResponse();
StreamReader stIn = new StreamReader(response.GetResponseStream());
string strResponse = stIn.ReadToEnd();
stIn.Close();
cookies = request.CookieContainer;
response.Close();
}
示例7: WriteMultipartBodyToRequest
private static void WriteMultipartBodyToRequest(HttpWebRequest request, string body)
{
string[] multiparts = Regex.Split(body, @"<!>");
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
foreach (string part in multiparts)
{
if (File.Exists(part))
{
bytes = File.ReadAllBytes(part);
}
else
{
bytes = System.Text.Encoding.UTF8.GetBytes(part.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n"));
}
ms.Write(bytes, 0, bytes.Length);
}
request.ContentLength = ms.Length;
using (Stream stream = request.GetRequestStream())
{
ms.WriteTo(stream);
}
}
}
示例8: UploadString
public static string UploadString(HttpWebRequest request, string Data, CookieCollection Cookies)
{
string ret = "";
request.CookieContainer = new CookieContainer();
foreach (Cookie cookie in Cookies)
request.CookieContainer.Add(cookie);
//request.Headers = Headers;
HttpWebResponse res;
request.Method = "POST";
//request.ContentLength = Data.Length;
using (Stream s = request.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(Data);
//s.Close();
sw.Close();
}
}
ret = GetString(request, out res);
return ret;
}
示例9: GetCookie
protected CookieContainer GetCookie(HttpWebRequest request, CookieContainer cc, byte[] content)
{
if (cc != null)
{
request.CookieContainer = cc;
}
else
{
request.CookieContainer = new CookieContainer();
cc = request.CookieContainer;
}
try
{
Stream stream = request.GetRequestStream();
stream.Write(content, 0, content.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
//CookieCollection cook = response.Cookies;
string CookiesString = request.CookieContainer.GetCookieHeader(request.RequestUri);
// TODO save cookiesstring to xml file
}
catch (WebException wex)
{
// TODO Log
}
catch (Exception ex)
{
// TODO log
}
return cc;
}
示例10: GetResponse
public static string GetResponse(HttpWebRequest req, byte[] bytes)
{
try
{
using (Stream stream = req.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = res.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
else
{
string message = String.Format("POST failed. Received HTTP {0}", res.StatusCode);
throw new ApplicationException(message);
}
}
}
catch(WebException e)
{
throw new RestHostMissingException(e);
}
}
示例11: DoRequest
private void DoRequest(string url, string postData) {
_request = (HttpWebRequest)WebRequest.Create("http://localhost" + url);
_request.CookieContainer = _cookieContainer;
if (postData != null) {
_request.Method = "POST";
_request.ContentType = "application/x-www-form-urlencoded";
using (var stream = _request.GetRequestStream()) {
using (var writer = new StreamWriter(stream)) {
writer.Write(postData);
}
}
}
try {
_response = (HttpWebResponse)_request.GetResponse();
}
catch (WebException ex) {
_response = (HttpWebResponse)ex.Response;
}
using (var stream = _response.GetResponseStream()) {
using (var reader = new StreamReader(stream)) {
_text = reader.ReadToEnd();
}
}
}
示例12: HeartBeatHandler
void HeartBeatHandler() {
while( true ) {
try {
request = (HttpWebRequest)WebRequest.Create( URL );
request.Method = "POST";
request.Timeout = 15000; // 15s timeout
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy( System.Net.Cache.RequestCacheLevel.NoCacheNoStore );
byte[] formData = Encoding.ASCII.GetBytes( staticData + "&users=" + world.GetPlayerCount() );
request.ContentLength = formData.Length;
using( Stream requestStream = request.GetRequestStream() ) {
requestStream.Write( formData, 0, formData.Length );
requestStream.Flush();
}
if( !hasReportedServerURL ) {
using( WebResponse response = request.GetResponse() ) {
using( StreamReader responseReader = new StreamReader( response.GetResponseStream() ) ) {
world.config.ServerURL = responseReader.ReadToEnd().Trim();
}
}
request.Abort();
hash = world.config.ServerURL.Substring( world.config.ServerURL.LastIndexOf( '=' ) + 1 );
world.FireURLChange( world.config.ServerURL );
hasReportedServerURL = true;
}
} catch( Exception ex ) {
world.log.Log( "HeartBeat: {0}", LogType.Error, ex.Message );
}
try {
request = (HttpWebRequest)WebRequest.Create( fListURL );
request.Method = "POST";
request.Timeout = 15000; // 15s timeout
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy( System.Net.Cache.RequestCacheLevel.NoCacheNoStore );
string requestString = staticData +
"&users=" + world.GetPlayerCount() +
"&hash=" + hash +
"&motd=" + Server.UrlEncode( world.config.GetString( "MOTD" ) ) +
"&server=fcraft" +
"&players=" + world.GetPlayerListString();
byte[] formData = Encoding.ASCII.GetBytes( requestString );
request.ContentLength = formData.Length;
using( Stream requestStream = request.GetRequestStream() ) {
requestStream.Write( formData, 0, formData.Length );
requestStream.Flush();
}
request.Abort();
} catch( Exception ex ) {
world.log.Log( "HeartBeat: Error reporting to fList: {0}", LogType.Error, ex.Message );
}
Thread.Sleep( Config.HeartBeatDelay );
}
}
示例13: handleValidResponse
private HttpWebResponse handleValidResponse(HttpWebRequest request, byte[] bytes)
{
using (var newStream = request.GetRequestStream()) {
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
}
return (HttpWebResponse) request.GetResponse();
}
示例14: CopyFile
public static void CopyFile(HttpWebRequest request, string file)
{
FileStream fileStream = File.OpenRead(file);
Stream rs = request.GetRequestStream();
Utils.CopyNBit (rs, fileStream, fileStream.Length);
fileStream.Close ();
rs.Close ();
}
示例15: InputPostData
void InputPostData(string postData, HttpWebRequest req)
{
byte[] data = Encoding.ASCII.GetBytes(postData);
req.ContentLength = data.Length;
using (var input = req.GetRequestStream()) {
input.Write(data, 0, data.Length);
}
}