demo代码块
<p>C#-RestSharp demo代码块</p>
<pre><code class="language-csharp">// C#-RestSharp
var options = new RestClientOptions(&quot;&quot;)
{
MaxTimeout = -1,
};
var client = new RestClient(options);
var request = new RestRequest(&quot;http://localhost/payment/gateway/api/backTransReq&quot;, Method.Post);
request.AddHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
request.AddParameter(&quot;merchantNo&quot;, &quot;800440050942370&quot;);
request.AddParameter(&quot;signType&quot;, &quot;MD5&quot;);
request.AddParameter(&quot;orderNo&quot;, &quot;001&quot;);
request.AddParameter(&quot;version&quot;, &quot;V1.0&quot;);
request.AddParameter(&quot;requestNo&quot;, &quot;001&quot;);
request.AddParameter(&quot;transId&quot;, &quot;04&quot;);
request.AddParameter(&quot;signature&quot;, &quot;xxx&quot;);
RestResponse response = await client.ExecuteAsync(request);
Console.WriteLine(response.Content);
</code></pre>
<p>JAVA示例代码块 demo</p>
<pre><code class="language-java">// JAVA HttpClient
String url = &quot;http://localhost/payment/gateway/api/backTransReq&quot;;
String orderId = &quot;Req&quot; + System.currentTimeMillis();
Map&lt;String, String&gt; maps = new TreeMap&lt;&gt;();
maps.put(&quot;requestNo&quot;, orderId);
maps.put(&quot;signType&quot;, &quot;MD5&quot;);
maps.put(&quot;version&quot;, &quot;V1.0&quot;);
maps.put(&quot;transId&quot;, &quot;04&quot;);
//商户号
maps.put(&quot;merchantNo&quot;, &quot;800440050942370&quot;);
maps.put(&quot;orderNo&quot;, &quot;R001&quot;);
String str = &quot;&quot;;
for (Map.Entry&lt;String, String&gt; m : maps.entrySet()) {
if (StringUtils.isEmpty(m.getValue())) {
continue;
}
str += m.getKey() + &quot;=&quot; + m.getValue() + &quot;&amp;&quot;;
}
//密钥
str = str + key;
String sign = getMd5(str);
maps.put(&quot;signature&quot;, sign);
HttpPost postMethod = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
postMethod.setConfig(requestConfig);
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.createDefault();
List&lt;BasicNameValuePair&gt; nvps = new ArrayList&lt;BasicNameValuePair&gt;();
for (Map.Entry&lt;String, String&gt; m : maps.entrySet()) {
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue()));
}
postMethod.setEntity(new UrlEncodedFormEntity(nvps, &quot;utf-8&quot;));
HttpResponse resp = httpClient.execute(postMethod);
String resp_str = EntityUtils.toString(resp.getEntity(), &quot;utf-8&quot;);
System.out.println(&quot;reqStr:&quot; + JSONObject.toJSONString(maps));
System.out.println(&quot;respStr:&quot; + resp_str);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}</code></pre>
<p>PHP示例代码块 demo</p>
<pre><code class="language-php">&lt;?php
public function sign($data = array(), $md5Key)
{
ksort($data);
$buffer = '';
foreach ($data as $key =&gt; $value) {
if ($value == '') continue;
$buffer .= &quot;$key=$value&amp;&quot;;
}
$buffer .= $md5Key;
return rtrim($buffer, '&amp;');
}
$signStr = $this-&gt;sign($data, $signKey);
//sign
$data['signature'] = md5($signStr);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =&gt; 'http://localhost/payment/gateway/api/backTransReq',
CURLOPT_RETURNTRANSFER =&gt; true,
CURLOPT_ENCODING =&gt; '',
CURLOPT_MAXREDIRS =&gt; 10,
CURLOPT_TIMEOUT =&gt; 0,
CURLOPT_FOLLOWLOCATION =&gt; true,
CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST =&gt; 'POST',
CURLOPT_POSTFIELDS =&gt; 'merchantNo=800440050942370&amp;signType=MD5&amp;orderNo=001&amp;version=V1.0&amp;requestNo=001&amp;transId=04&amp;signature=xxx',
CURLOPT_HTTPHEADER =&gt; array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
</code></pre>