HttpRequest的使用之Python VS Java

主要想对最近分别用Python和Java去实现HttpRequest做个记录总结:

Python:common类里面定义好了get,post

import requests


# 定义一个common的类,它的父类是object
class Common(object):
  # common的构造函数
  def __init__(self):
    # 被测系统的根路由
    self.url_root = 'http://localhost:3000'
  # 封装你自己的get请求,uri是访问路由,params是get请求的参数,如果没有默认为空
  def get(self, uri, params=''):
    # 拼凑访问地址
    url = self.url_root + uri + params
    # 通过get请求访问对应地址
    res = requests.get(url)
    # 返回request的Response结果,类型为requests的Response类型
    return res
  # 封装你自己的post方法,uri是访问路由,params是post请求需要传递的参数,如果没有参数这里为空
  def post(self, uri, params=''):
    # 拼凑访问地址
    url = self.url_root + uri
    if len(params) > 0:
      # 如果有参数,那么通过post方式访问对应的url,并将参数赋值给requests.post默认参数data
      # 返回request的Response结果,类型为requests的Response类型
      res = requests.post(url, data=params)
    else:
      # 如果无参数,访问方式如下
      # 返回request的Response结果,类型为requests的Response类型
      res = requests.post(url)
    return res
View Code

其中调用get:

运行效果如下:

 调用post,运行效果如下:

 Part2: 会跟新上java中实现

猜你喜欢

转载自www.cnblogs.com/jessicaxia/p/13183597.html