用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

Http的POST-FORM格式传输数据

2016-09-21 作者: 疯信子举报

[c#]代码库

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
public class HttpUtil
{
    /// <summary>
    /// 默认按keyValuePair的方式传输 http POST 
    /// </summary>
    /// <param name="postUrl">URL</param>
    /// <param name="postDataStr">数据</param>
    /// <returns>返回内容</returns>
    public static ReturnInfo SendPost(string postUrl, string postDataStr, string contentType = "application/x-www-form-urlencoded")
    {
        System.Net.ServicePointManager.DefaultConnectionLimit = 100;
        System.Net.ServicePointManager.Expect100Continue = false;
        ReturnInfo _returnInfo = new ReturnInfo();

        //用来存放cookie
        HttpWebRequest request = null;
        Stream myRequestStream = null;
        HttpWebResponse response = null;
        Stream myResponseStream = null;
        StreamReader myStreamReader = null;
        try
        {
            //转化
            Console.WriteLine("httppost:" + postDataStr);
            byte[] byteArray = Encoding.UTF8.GetBytes(postDataStr);
            //发送一个POST请求
            request = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
            request.Timeout = 30000;
            request.Method = "POST";
            if (contentType == null || contentType == "")
            {
                contentType = "application/x-www-form-urlencoded";
            }
            request.ContentType = contentType;
            request.ContentLength = byteArray.Length;
            myRequestStream = request.GetRequestStream();
            myRequestStream.Write(byteArray, 0, byteArray.Length);
            myRequestStream.Close();
            //获取返回的内容
            response = (HttpWebResponse)request.GetResponse();
            myResponseStream = response.GetResponseStream();
            myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            _returnInfo.Successful = true;
            _returnInfo.ErrorMsg = "";
            _returnInfo.ReturnObj = myStreamReader.ReadToEnd();
        }
        catch (Exception ex)
        {
            _returnInfo.Successful = false;
            _returnInfo.ErrorMsg = ex.Message;
            _returnInfo.ReturnObj = null;
        }
        finally
        {
            if (myStreamReader != null)
            {
                myStreamReader.Close();
            }
            if (myResponseStream != null)
            {
                myResponseStream.Close();
            }
            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }
        }
        return _returnInfo;
    }

    /// <summary>
    /// 将数据组装成 key=UrlEncode(value)&key2=UrlEncode(value2),并httpPost
    /// </summary>
    /// <param name="postUrl">要发送的目标地址URI</param>
    /// <param name="postData">keyValuePair</param>
    /// <returns>传输结果</returns>
    public static ReturnInfo SendPost(string postUrl, Dictionary<string, string> postData)
    {
        StringBuilder sb = new StringBuilder();
        List<string> list = new List<string>();
        foreach (var item in postData)
        {
            if (item.Value == null)
            {
                continue;
            }
            list.Add(item.Key + "=" + HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8));
        }
        return SendPost(postUrl, string.Join("&", list), "");
    }
}

public class ReturnInfo
{
    public ReturnInfo() { }
    public ReturnInfo(bool successful, string errormsg, object returnobj = null)
    {
        this.Successful = successful;
        this.ErrorMsg = errormsg;
        this.ReturnObj = returnobj;
    }
    public bool Successful { get; set; }
    public string ErrorMsg { get; set; }
    public object ReturnObj { get; set; }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...