Post方法示例
.net(c#)示例
1
var jsonData = "{\"password\":\"\",\"staffCode\":\"\",\"timeStamp\":\"\",\"realName\":\"\",\"passwordType\":\"0\",\"companyId\":\"\"}";
var host = "http://testapi.shinetour.com";
var method = "/api.svc/login";
var result = "";
var url = host + method;
using (var client = new HttpClient())
{
var param = new StringContent(jsonData);
//指定数据类型为Json
param.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
//请求结果
result = client.PostAsync(url, param).Result.Content.ReadAsStringAsync().Result;
}
2
var jsonData = "{\"password\":\"\",\"staffCode\":\"\",\"timeStamp\":\"\",\"realName\":\"\",\"passwordType\":\"0\",\"companyId\":\"\"}";
var host = "http://testapi.shinetour.com";
var method = "/api.svc/login";
var result = "";
var url = host + method;
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(url);
//指定请求方式为post
request.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(jsonData);
request.ContentType = "application/json;charset=utf-8";
request.ContentLength = byte1.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
newStream.Close();
var result = "";
//开始请求
using (var response = (HttpWebResponse)request.GetResponse())
{
Encoding enc = Encoding.GetEncoding("UTF-8");
using (var stream = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(stream, enc);
result = streamReader.ReadToEnd();
}
}
Java示例
1
//导入以下包
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
JSONObject jsonObject = new JSONObject();
jsonObject.put("password", "");
jsonObject.put("staffCode", "");
jsonObject.put("timeStamp", "");
jsonObject.put("realName", "");
jsonObject.put("passwordType", "");
jsonObject.put("companyId", "");
String host = "http://testapi.shinetour.com";
String method = "/api.svc/login";
String url = host + method;
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
//指定为json
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
StringEntity entity;
String result = "";
try {
entity = new StringEntity(jsonObject.toJSONString(), "UTF-8");
// 创建带字符创参数和字符编码的
httpPost.setEntity(entity);
HttpResponse httpResponse;
// 开始请求
httpResponse = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
//请求结果
result = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
2
//导入以下包
import java.util.Scanner;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
JSONObject jsonObject = new JSONObject();
jsonObject.put("password", "");
jsonObject.put("staffCode", "");
jsonObject.put("timeStamp", "");
jsonObject.put("realName", "");
jsonObject.put("passwordType", "");
jsonObject.put("companyId", "");
String data = jsonObject.toJSONString();
HttpClient http = new HttpClient();
String method = "/api.svc/login";
String result = "";
String url = host + method;
PostMethod post = new PostMethod(url);
post.addRequestHeader("Content-Type", "application/json; charset=UTF-8");
if (data != null)
post.setRequestEntity(new StringRequestEntity(data, "application/json", "UTF-8"));
StringBuilder res = new StringBuilder();
int statusCode = http.executeMethod(post);
String encode = post.getResponseCharSet() == null ? "UTF-8" : post.getResponseCharSet();
Scanner scan = new Scanner(post.getResponseBodyAsStream(), encode);
while (scan.hasNextLine())
res.append(scan.nextLine());
//请求结果
result=res.toString();
Python示例
1
# 示例使用的为 python 3.6
# 导入以下包
import datetime
import hashlib
import json
import requests
def login_demo():
company_id = "" # 美亚提供的 公司编号
staff_code = "" # 员工工号
real_name = "" # 员工姓名
time_stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
sign_key="" # 美亚提供的 key
host = "http://121.41.36.97:6005/"
# 加密 begin
# 排序
str_arr = [company_id, staff_code, real_name, time_stamp, sign_key]
str_arr.sort(key=str.lower)
data = ''.join(str_arr)
sha = hashlib.sha1()
data = data.encode("utf-8")
sha.update(data)
# 加密结果
password = sha.hexdigest()
# 加密 end
# 参数
str_json = {
"password": password,
"timeStamp": time_stamp,
"realName": real_name,
"passwordType": "0",
"companyId": company_id,
"staffCode": staff_code
}
# 开始请求
headers = {"Content-Type": "application/json"}
response = requests.post(host + "api.svc/login", json.dumps(str_json), headers=headers)
print(response)
response_json = json.loads(response.text)
print(response_json)
if response_json['code'] == '10000':
# 成功
print('success')
else:
# 失败
print('error')