System.Net.WebException: 未能解析此远程名称‘api.weixin.qq.com‘

        在服务器上调用第三方接口,出现异常报错"System.Net.WebException: 未能解析此远程名称: 'api.weixin.qq.com'"等此类问题。解决方法如下

        1、使用CMD的命令窗口Ping 第三方接口的域名获得域名的真实IP。

        2、服务器上找到指定文件夹(C:\Windows\System32\drivers\etc\ )中 hosts文件。

        3、打开hosts文件,在末尾添加上IP与域名的绑定对应关系,然后保存hosts文件即可。

        例如:119.147.6.237      api.weixin.qq.com 。

 

 

### ASP.NET 中实现微信支付退款功能 在 ASP.NET 应用程序中实现微信支付的退款操作涉及多个步骤,包括但不限于发送请求到微信服务器并处理响应。下面是一个简化版的代码示例来展示如何执行此过程: ```csharp using System; using System.IO; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Text; public class WeChatPayRefundService { private string _appId; // 微信分配的小程序ID private string _mchId; // 商户号 private string _apiKey; // API密钥 public void Refund(string outTradeNo, decimal totalFee, decimal refundFee) { var url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; var xmlRequest = new StringBuilder(); xmlRequest.AppendLine("<xml>"); xmlRequest.AppendFormat("<appid>{0}</appid>", _appId); xmlRequest.AppendFormat("<mch_id>{0}</mch_id>", _mchId); xmlRequest.AppendFormat("<out_trade_no>{0}</out_trade_no>", outTradeNo); xmlRequest.AppendFormat("<total_fee>{0}</total_fee>", Convert.ToInt32(totalFee * 100)); // 单位转换成分为整数形式 xmlRequest.AppendFormat("<refund_fee>{0}</refund_fee>", Convert.ToInt32(refundFee * 100)); xmlRequest.AppendFormat("<op_user_id>{0}</op_user_id>", _mchId); // 操作员账号,默认为商户号 xmlRequest.AppendLine("</xml>"); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; byte[] byteArray = Encoding.UTF8.GetBytes(xmlRequest.ToString()); request.Method = "POST"; request.ContentType = "application/xml"; request.ClientCertificates.Add(new X509Certificate("apiclient_cert.p12", _mchId)); using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } try { HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream streamResponse = response.GetResponseStream(); StreamReader reader = new StreamReader(streamResponse); Console.WriteLine(reader.ReadToEnd()); streamResponse.Close(); reader.Close(); } catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Forbidden || (ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound || (ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.BadRequest) { using (var errorReader = new StreamReader(ex.Response.GetResponseStream())) { Console.WriteLine(errorReader.ReadToEnd()); } } } } ``` 上述代码展示了发起一次退款请求的过程[^1]。 为了使这段代码正常工作,还需要确保已经配置好了相应的证书以及设置了正确的参数值(如`_appId`, `_mchId`, 和 `_apiKey`)。此外,在实际项目里应该加入更多的异常处理机制和服务端验证逻辑以提高系统的健壮性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值