C#解析"a=1&b=2&c=3"字符串,微信支付返回字符串,替换<br>为&

原文来自: http://www.mzwu.com/article.asp?id=2802

C#可用:

若该字符串是使用Http Get发送,url?a=1&b=2&c=3,使用下边代码即可获取参数a的值:

程序代码
Request.QueryString["a"]


若该字符串是远程接口返回,以前都是用Split函数去拆分,今天发现一个非常强大的方法ParseQueryString,简单多了:

 程序代码
<%@ WebHandler Language="C#" class="Default" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Specialized;

public class Default : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string str = "a=1&b=2&c=3";
        NameValueCollection query = HttpUtility.ParseQueryString(str, Encoding.GetEncoding("gb2312"));
        context.Response.Write(query["a"]);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

C#例子代码:

            resultDescription = resultDescription.Replace("<br>", "&");
            NameValueCollection query = HttpUtility.ParseQueryString(resultDescription, Encoding.GetEncoding("gb2312"));
            responseModel.MicroPayRequestModel = new Models.RequestModel.MicroPayRequestModel();
            responseModel.MicroPayRequestModel.Appid = query["appid"];
responseModel.MicroPayRequestModel.XXX= query["XXX"];

 

posted @ 2017-11-14 10:53  歌·颂  阅读(641)  评论(0编辑  收藏  举报