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"];

 

转载于:https://www.cnblogs.com/Early-Bird/p/7831360.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值