资源简介
利用C#实现HTTP GET和POST方法的样例代码,供大家学习。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace MongoDB_Test
{
class Program
{
static CookieContainer cookie = new CookieContainer();
static void Main(string[] args)
{
string url = “http://127.0.0.1:9980“;
string data = “key=1054228&value=test“;
string result1 = HttpGet(url data);
Console.WriteLine(result1.Length + “\n“ + result1);
//string result2 = HttpPost(url data);
//Console.WriteLine(result2.Length + “\n“ + result2);
Console.ReadLine();
}
private static string HttpPost(string Url string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = “POST“;
request.ContentType = “application/x-www-form-urlencoded“;
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream Encoding.GetEncoding(“gb2312“));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream Encoding.GetEncoding(“utf-8“));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
private static string HttpGet(string Url string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == ““ ? ““ : “?“) + postDataStr);
request.Method = “GET“;
request.ContentType = “text/html;charset=UTF-8“;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream Encoding.GetEncoding(“utf-8“));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 161 2017-05-09 11:38 HTTP_Demo\ConsoleApplication1\app.config
文件 6656 2016-05-25 20:45 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
文件 161 2017-05-09 11:38 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.config
文件 13824 2016-05-25 20:45 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
文件 23168 2018-10-26 16:13 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe
文件 161 2017-05-09 11:38 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config
文件 490 2018-04-12 07:35 HTTP_Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest
文件 368640 2015-12-09 16:43 HTTP_Demo\ConsoleApplication1\bin\Debug\MongoDB.Bson.dll
文件 529408 2015-12-09 16:43 HTTP_Demo\ConsoleApplication1\bin\Debug\MongoDB.Driver.dll
文件 2525 2017-05-24 10:17 HTTP_Demo\ConsoleApplication1\ConsoleApplication1.csproj
文件 1804 2018-10-26 16:13 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt
文件 1755 2016-05-25 15:01 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.csprojResolveAssemblyReference.cache
文件 6656 2016-05-25 20:45 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.exe
文件 13824 2016-05-25 20:45 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.pdb
文件 6494 2017-05-23 17:50 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 2655 2016-05-25 20:05 HTTP_Demo\ConsoleApplication1\Program.cs
文件 1382 2017-04-17 19:45 HTTP_Demo\ConsoleApplication1\Properties\AssemblyInfo.cs
..A..H. 15872 2017-04-17 19:51 HTTP_Demo\ConsoleApplication1.suo
..A..H. 34816 2017-05-16 15:57 HTTP_Demo\ConsoleApplication1.v12.suo
文件 899 2017-04-17 19:46 HTTP_Demo\HTTP_GetPost.sln
..A..H. 30208 2018-10-26 16:15 HTTP_Demo\HTTP_GetPost.v12.suo
目录 0 2017-04-17 19:45 HTTP_Demo\ConsoleApplication1\obj\x86\Debug\TempPE
目录 0 2016-05-25 20:45 HTTP_Demo\ConsoleApplication1\obj\x86\Debug
目录 0 2016-05-25 15:01 HTTP_Demo\ConsoleApplication1\bin\Debug
目录 0 2017-05-23 17:49 HTTP_Demo\ConsoleApplication1\obj\x86
目录 0 2017-05-23 17:49 HTTP_Demo\ConsoleApplication1\bin
目录 0 2017-05-23 17:49 HTTP_Demo\ConsoleApplication1\obj
目录 0 2017-05-23 17:49 HTTP_Demo\ConsoleApplication1\Properties
目录 0 2016-05-25 20:05 HTTP_Demo\ConsoleApplication1
目录 0 2016-05-25 20:53 HTTP_Demo
............此处省略3个文件信息
- 上一篇:C#上位机控制8路继电器
- 下一篇:C#停车管理系统
相关资源
- HTTPS证书创建+绑定端口+C#程序监听
- C# Winform 简单实现Http
- HttpMonitor的完整源代码,方便获取re
- C#(Csharp)多线程HTTP并发请求(Http
- C#使用HttpClient
- WCF 解析 HTTP Multipart Form Data
- c# http接口设计及调用demo
- C# winhttp 封装类
- C# WEB/HTTP大文件与文件夹上传2
- asp.net c# http https 代理服务器源码
- C#开发webservice接口,对客户端post服务
- c#实现HttpClient拼接multipart/form-data形式
- Http APi 测试工具
- C#服务器推技术http长连接的应用
- C#HttpWebRequest大文件断点续传类
- C# asp.net http HttpWebRequest模拟浏览器请
- winform程序仿迅雷
- HttpHelper万能框架V2.4.zip
- 接收发送HTTP报文
- 如何在WPF应用程序中通过HttpClient调用
- C# HttpHelper.cs
- C# httplistener 简单的高并发http服务器
- C#网络应用编程第三版马骏主编上机练
- C#版支持高并发的HTTP服务器源码
- ASP.NET System.Web.Http.dll
- C# Http协议上传文件有进度条
- WinformC# 模拟登陆百度 HttpWebRequest的应
- c#下的http请求通用类(自动实现保留
- C#通过winform和http上传文件
- C#HTTPclient 应用
评论
共有 条评论