Last active
June 2, 2019 04:02
-
-
Save guerbai/1d11852e39ad985bf393a4846d594286 to your computer and use it in GitHub Desktop.
代码请求客户端
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, request | |
app = Flask(__name__) | |
@app.route("/getdata", methods=["GET", "POST"]) | |
def getdata(): | |
bookinfo = { | |
"lie": True, | |
"booklist": [ | |
{ | |
"name": "renyueshenhua", | |
"bookid": 1, | |
}, | |
{ | |
"name": "bianchengzhuji", | |
"bookid": 2, | |
}, | |
{ | |
"name": "chonggou", | |
"bookid": 3, | |
} | |
], | |
"totalNums": 3, | |
"properties": "bookinfo", | |
} | |
if request.method == "POST": | |
postdata = dict(request.json) | |
bookinfo["booklist"].append(postdata) | |
bookinfo["totalNums"] += 1 | |
return jsonify(bookinfo) | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetTest { | |
public static void main(String[] args){ | |
String targetUrl = "http://localhost:5000/getdata"; | |
HttpClient httpClient = HttpClientBuilder.create().build(); | |
HttpGet getRequest = new HttpGet(targetUrl); | |
getRequest.addHeader("Accept", "application/json"); | |
try { | |
HttpResponse response = httpClient.execute(getRequest); | |
int resCode = response.getStatusLine().getStatusCode(); | |
System.out.println(resCode); | |
HttpEntity httpEntity = response.getEntity(); | |
String resContent = EntityUtils.toString(httpEntity, "UTF-8"); | |
// System.out.println(resContent); | |
JsonParser jsonParser = new JsonParser(); | |
JsonObject jsonObject = (JsonObject) jsonParser.parse(resContent); | |
JsonArray booklist = jsonObject.get("booklist").getAsJsonArray(); | |
System.out.println(booklist.get(2).getAsJsonObject().get("name")); | |
int totalNums = jsonObject.get("totalNums").getAsInt(); | |
System.out.println(totalNums); | |
boolean lie = jsonObject.get("lie").getAsBoolean(); | |
System.out.println(lie); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetTest { | |
public static void main(String[] args){ | |
String targetUrl = "http://localhost:5000/getdata"; | |
HttpClient httpClient = HttpClientBuilder.create().build(); | |
HttpPost PostRequest = new HttpPost(targetUrl); | |
JsonObject postJson = new JsonObject(); | |
postJson.addProperty("params", "1"); | |
StringEntity stringEntity = new StringEntity(postJson.toString(), "UTF-8"); | |
postRequest.setEntity(strinEntity); | |
postRequest.setHeader("Accept", "application/json"); | |
postRequest.setHeader("Content-Type", "application/json"); | |
try { | |
HttpResponse response = httpClient.execute(postRequest); | |
int resCode = response.getStatusLine().getStatusCode(); | |
System.out.println(resCode); | |
HttpEntity httpEntity = response.getEntity(); | |
String resContent = EntityUtils.toString(httpEntity, "UTF-8"); | |
// System.out.println(resContent); | |
JsonParser jsonParser = new JsonParser(); | |
JsonObject jsonObject = (JsonObject) jsonParser.parse(resContent); | |
JsonArray booklist = jsonObject.get("booklist").getAsJsonArray(); | |
System.out.println(booklist.get(2).getAsJsonObject().get("name")); | |
int totalNums = jsonObject.get("totalNums").getAsInt(); | |
System.out.println(totalNums); | |
boolean lie = jsonObject.get("lie").getAsBoolean(); | |
System.out.println(lie); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import json | |
import pprint | |
def _test_api(): | |
url = "http://127.0.0.1:5000/api" | |
headers = {'content-type': 'application/json'} | |
payload = { | |
} | |
pprint.pprint(json.dumps(payload)) | |
response = requests.post(url, data=json.dumps(payload), headers=headers).json() | |
pprint.pprint(response) | |
def _test_post_api(): | |
url = "http://127.0.0.1:8080/bootpost" | |
# headers = {'content-type': 'application/x-www-form-urlencoded'} | |
headers = {'content-type': 'application/json'} | |
payload = { | |
"id": "1233123", | |
"type": "youknow", | |
"data": { | |
"v1": [1,2,3], | |
"v2": True, | |
"v3": "putyou" | |
} | |
} | |
pprint.pprint(json.dumps(payload)) | |
response = requests.post(url, data=json.dumps(payload), headers=headers).json() | |
# response = requests.get(url, headers=headers).json() | |
pprint.pprint(response) | |
if __name__ == '__main__': | |
_test_api() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import json | |
import pprint | |
def _test_api(): | |
url = "http://127.0.0.1:5000/api" | |
headers = {'content-type': 'application/json'} | |
payload = { | |
} | |
pprint.pprint(json.dumps(payload)) | |
response = requests.post(url, data=json.dumps(payload), headers=headers).json() | |
pprint.pprint(response) | |
if __name__ == '__main__': | |
_test_api() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootApplication | |
@RestController | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@GetMapping("/boot/{username}") | |
public ResEntity boot(@PathVariable String username) { | |
ResEntity resEntity = new ResEntity(); | |
resEntity.setKey(username); | |
Map<String, Object> map = new HashMap<String, Object>(); | |
map.put("key3", "value3"); | |
resEntity.setKey2(map); | |
int[] list = {1, 2, 3}; | |
resEntity.setList(list); | |
return resEntity; | |
} | |
@GetMapping("/boot2") | |
public Map<String, Object> boot2() { | |
Map<String, Object> map = new HashMap<String, Object>(); | |
map.put("key", "value"); | |
map.put("key2", new int[]{1, 3, 5}); | |
return map; | |
} | |
@PostMapping("/bootpost") | |
public String bootpost(@RequestBody Map<String, Object> param) { | |
if (param.containsKey("id")) { | |
System.out.println(param.get("id")); | |
} | |
System.out.println(param); | |
return "hello"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment