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 ();
}
}
}
分享到:
相关推荐
主要介绍了C#实现发送简单HTTP请求的方法,涉及C#操作http的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
C#发送HTTP请求,实现登录、查询. // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest;//定义一个HTTP请求 CookieContainer cookieContainer = new CookieContainer();//定义一个Cookies容器 ...
c# http协议,实现get或post发送请求 并返回内容
主要介绍了c#使用Socket发送HTTP/HTTPS请求的实现代码,需要的朋友可以参考下
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
本文主要介绍了使用C#发送Http请求实现模拟登陆实例,模拟登陆的原理简单,想要了解的朋友可以了解一下。
C# winform开发基础,使用http发送请求的demo样例。 适合初学者使用。 代码完整,visual studio 2019下打开即可运行。 C#下http的调用方式;json格式化的解析等。 需要个后台搭配使用,对于新手来说可能麻烦点。如有...
这个是从C#窗体后台发送HTTP请求用的,如果是ASP.net的话,可以直接用http对象发送对象
代码中包含知识点:异步请求HTTP,GET,POST,PUT,DELETE,JSON序列化与反序列化
自己封装的基于C#的发送HTTP请求。包括普通POST\GET\FILE形式。 获取HTML:var hp = new HttpHelper("http://www.cnblogs.com"); hp.Send(); 发送POST:var hp = new HttpHelper("http://www.cnblogs.com/upload",...
C#中用SOCKET发送HTTP请求小例 自行组织http报文,报显示服务器反馈回来的报头与报体信息。 ------------- HTTP/1.1 200 OK Date: Mon, 08 Dec 2008 14:20:51 GMT Server: BWS/1.0
Http的同步请求用C#简单的编写,下下看看,提点意见。
C#的http发送post和get请求源码,非常轻量简单,测试的时候简单实用下挺不错
一个C#的winform程序 主要功能为抓取本地的Access数据库数据 构造json。模拟请求发送到java后台并接受返回值
如何伪造IP 发送HTTP请求request.zip
C#发送http请求的高效工具,比原生的HttpWebRequest好用。
在C#中发送HttpPost请求来调用WebService中的MyAction方法,代码如下:需要的朋友可以参考一下
C#用POST方式发送数据到云服务器的时候,传输的文件大小会受到限制,文档给出了解决方法。亲测可行
手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法 手工发送HTTP的GET请求: string strURL = http://localhost/Play/CH1/Service1.asmx/doSearch?keyword=; strURL +=this.textBox1.Text; System.Net....