• 大小: 333KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C#
  • 标签: http  

资源简介

利用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个文件信息

评论

共有 条评论