1:使用WebRequest向服务器发送GET请求,获取服务器的响应
1. 创建请求对象
2. 获取web响应
3. 获取web响应流
4.读取Web 流数据
完整代码如下:
var url ="http://webcode.me";//"http://www.baidu.com";
var request = WebRequest.Create(url);
request.Method = "GET";
using (var webResponse = request.GetResponse())
{
using (var webStream = webResponse.GetResponseStream())
{
using (var reader = new StreamReader(webStream))
{
var data = reader.ReadToEnd();
Console.WriteLine(data);
}
}
}
输出:
Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
2:使用WebRequest向服务器发送POST请求,获取服务器的响应
1. 创建Web请求对象
2. 指定POST方法
3. 创建POST数据对象
4. 将数据对象序列化为JSON字符串
5. 再将Json字符串转换为字节数组
6. 指定请求ContentType类型
7. 获取请求流对象
8. 将转换后的字节数组写入到请求流中
9. 获取请求的响应
10. 读取响应流中的数据
完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.IO;
namespace _02_HTTP_POST
{
//HTTP POST
//The HTTP POST method sends data to the server.
//It is often used when uploading a file or when submitting a completed web form.
class Program
{
//C# POST request with WebRequest
//creates a POST request with WebRequest.
static void Main(string[] args)
{
var url = "https://httpbin.org/post"; //发POST请求的服务器地址
var request = WebRequest.Create(url);
request.Method = "POST";
var user = new User("John Doe", "gardener");
var json = JsonConvert.SerializeObject(user);
byte[] byteArray = Encoding.UTF8.GetBytes(json);//序列化user对象为Json字符串,再将Json字符串转为字节数组
request.ContentType = "application/json";
//request.ContentType = "application/x-www-form-urlencoded";
//默认地,表单数据会编码为 "application/x-www-form-urlencoded"。
//就是说,在发送到服务器之前,所有字符都会进行编码(空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值)。
request.ContentLength = byteArray.Length;
using (var reqStream = request.GetRequestStream())//获取请求流对象
{
reqStream.Write(byteArray, 0, byteArray.Length);//将POST的内容传入到请求流
using (var response = request.GetResponse())// 获取响应的对象
{
Console.WriteLine(((HttpWebResponse)response).StatusDescription);// 响应状态
using (var respStream = response.GetResponseStream())// 从响应流中读取数据
{
using (var reader = new StreamReader(respStream))
{
string data = reader.ReadToEnd();
Console.WriteLine(data);
}
}
}
}
}
}
class User
{
public string Name { get; set; }
public string Occupation { get; set; }
public User(string name, string occupation)
{
Name = name;
Occupation = occupation;
}
public override string ToString()
{
return $"{Name}: {Occupation}";
}
}
}
输出:
OK
{
"args": {},
"data": "{\"Name\":\"John Doe\",\"Occupation\":\"gardener\"}",
"files": {},
"form": {},
"headers": {
"Content-Length": "43",
"Content-Type": "application/json",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-61aea836-517d25d562b9453c66922bab"
},
"json": {
"Name": "John Doe",
"Occupation": "gardener"
},
"origin": "180.161.88.74",
"url": "https://httpbin.org/post"
}
改变request.ContentType = "application/x-www-form-urlencoded";
输出:
OK
{
"args": {},
"data": "",
"files": {},
"form": {
"{\"Name\":\"John Doe\",\"Occupation\":\"gardener\"}": ""
},
"headers": {
"Content-Length": "43",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-61aea885-2ec9f6f639d59dba53b244ad"
},
"json": null,
"origin": "180.161.88.74",
"url": "https://httpbin.org/post"
}
差异: