|
|
using System;
|
|
|
using System.IO;
|
|
|
using System.Text;
|
|
|
using System.Net;
|
|
|
using System.Threading;
|
|
|
|
|
|
namespace BWP.ABCClient
|
|
|
{
|
|
|
public sealed class MockWebServer : IDisposable
|
|
|
{
|
|
|
private readonly HttpListener _listener;
|
|
|
private readonly Thread _thread;
|
|
|
private readonly Encoding _reqEncoding;
|
|
|
private Encoding _respEncoding;
|
|
|
private readonly bool _compressResp;
|
|
|
|
|
|
public MockWebServer() : this("http://localhost:5000/", Encoding.UTF8, Encoding.UTF8, false)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
public MockWebServer(string prefix, Encoding reqEncoding, Encoding respEncoding, bool compressResp)
|
|
|
{
|
|
|
try {
|
|
|
_listener = new HttpListener();
|
|
|
_listener.Prefixes.Add(prefix);
|
|
|
_listener.Start();
|
|
|
Console.WriteLine("MockWebServer Listening... ");
|
|
|
_thread = new Thread(Listen);
|
|
|
_thread.Start();
|
|
|
} catch (Exception e) {
|
|
|
Console.WriteLine(e);
|
|
|
}
|
|
|
_reqEncoding = reqEncoding;
|
|
|
_respEncoding = respEncoding;
|
|
|
_compressResp = compressResp;
|
|
|
}
|
|
|
|
|
|
private void Listen()
|
|
|
{
|
|
|
while (true) {
|
|
|
HttpListenerContext context = _listener.GetContext();
|
|
|
|
|
|
HttpListenerRequest request = context.Request;
|
|
|
string receivedData;
|
|
|
using (var inputStream = request.InputStream) {
|
|
|
using (var inputReader = new StreamReader(inputStream, _reqEncoding)) {
|
|
|
receivedData = inputReader.ReadToEnd();
|
|
|
}
|
|
|
}
|
|
|
Console.WriteLine("ReceivedData:");
|
|
|
Console.WriteLine(receivedData);
|
|
|
string responseStr = string.Empty;
|
|
|
if (request.RawUrl.Contains("B2C")) {
|
|
|
responseStr = "<MSG><Message><Merchant><ECMerchantType>B2C</ECMerchantType><MerchantID>103452083980410</MerchantID></Merchant><TrxResponse><ReturnCode>0000</ReturnCode><ErrorMessage>交易成功</ErrorMessage><TrxType>PayReq</TrxType><OrderNo>634333749713468745</OrderNo><PaymentURL>https://easyabc.95599.cn/b2c/NotCheckStatus/PaymentModeAct.ebf?TOKEN=12977496798933397376</PaymentURL><OrderAmount>170940.8</OrderAmount></TrxResponse></Message><Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>eAy2xu/VmFnkHEY5sAxwmV/NfbXUDRzLH3IJa7oXOMuGGHHkXfYlwFfe2HesKq6gQgfFcfIsNs0bEc8r6EDkzZVETuiVviQxahqnqr0+GRnfly5OwNELtFCbtJ5PdzuWjme6SbNPSTqMUN6bRWzgr/UXrkuo3uyJOIIyCFGsFbs=</Signature></MSG>";
|
|
|
_respEncoding = Encoding.GetEncoding("gb2312");
|
|
|
}
|
|
|
if (request.RawUrl.Contains("B2B")) {
|
|
|
responseStr = "<MSG><Message><Merchant><ECMerchantType>B2B</ECMerchantType><MerchantID>337100000000066</MerchantID></Merchant><TrxResponse><ReturnCode>0000</ReturnCode><ErrorMessage>交易成功</ErrorMessage><TrnxType>FundTransfer</TrnxType><TrnxAMT>.1</TrnxAMT><MerchantTrnxNo>634331972002042500</MerchantTrnxNo><PaymentURL>https://easyabc.95599.cn/b2b/NotCheckStatus/PaymentModeAct.ebf?TOKEN=12975716546177309876</PaymentURL></TrxResponse></Message><Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>977AkRZUxtHflGP4fyvfNFpcmjuD0goKvRWD+w5g/bx45ZiCkGIki3wDwtgSNBA+QDBs/cyYAsR6AxuIs9CC0obMeSMrMBbixKJgJkNKO0UPlIU4seJ5rSlYKT1HX7tNE1SI7/VEmsv9i0r110hQjkSjzFhVrv9jRlMhNxSopbg=</Signature></MSG>";
|
|
|
}
|
|
|
if (request.RawUrl.Contains("Market")) {
|
|
|
responseStr = "<MSG><Message><Control><MerchantTrxNo>RSU110406001</MerchantTrxNo><ChannelType>01</ChannelType><Version>Java_V1.0</Version><ClientIP>187.61.1.1</ClientIP><BusinessID>MARKET</BusinessID><FunctionID>0000</FunctionID><ReturnCode>0000</ReturnCode><MerchantID>337199901033E01</MerchantID></Control><Parameters></Parameters><Resultsets></Resultsets></Message><Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>evJk/wQM5BjR72vHSCd1wgXSK8ZSRCVHbKv15faI1dY5kr4oz7ThBftaqcKqV7548QoDrMT0M3nITOffUU2CkLqb4TJcLAFZm+UVX2p/+1J2VPR0pBOnVLde8F6ADtnSV4IvqOCSkRi1Zoymr6UQ1gVw9p1Mh+TgOd6K85Zhitk=</Signature></MSG>";
|
|
|
}
|
|
|
if (request.RawUrl.Contains("MarketOrderApply")) {
|
|
|
responseStr = "<MSG><Message><Control><MerchantTrxNo>Req110607008</MerchantTrxNo><ReturnMessage>交易成功</ReturnMessage><ChannelType>01</ChannelType><Version>Java_V1.0</Version><BusinessID>MARKET</BusinessID><FunctionID>0025</FunctionID><ReturnCode>0000</ReturnCode><MerchantID>337199901033E01</MerchantID></Control><Parameters><OrderDate>2011-06-07 11:13:18</OrderDate><ReturnURL>https://www.95599.cn/market/MarketOrderPay.ebf?token=13074163984216493171</ReturnURL></Parameters><Resultsets></Resultsets></Message><Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>MI99KF8wCoZR9DDBVEZWPFPJqJJFHV4Eci7/LJKP46fmaK3N4y9nWXPhJ+STnAZ5xwvjpRrgZqzcwVEY4acI0EqaUfADvzVV5jIgXatFG8gO1UhL3YpfK7HJX5lXdBcSN72QNuJA1M9ZbaH6KDhs3V/fWYrlLfTrdHnfjKssxMY=</Signature></MSG>";
|
|
|
}
|
|
|
|
|
|
if (_compressResp) {
|
|
|
responseStr = MsgUtil.Compress(responseStr);
|
|
|
}
|
|
|
HttpListenerResponse response = context.Response;
|
|
|
byte[] buffer = _respEncoding.GetBytes(responseStr);
|
|
|
response.ContentLength64 = buffer.Length;
|
|
|
using (Stream output = response.OutputStream) {
|
|
|
output.Write(buffer, 0, buffer.Length);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
|
{
|
|
|
_thread.Abort();
|
|
|
_listener.Stop();
|
|
|
}
|
|
|
}
|
|
|
}
|