Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rym-util
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengqiuyun
rym-util
Commits
179a4ecc
Commit
179a4ecc
authored
Oct 26, 2022
by
zhengqiuyun86
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数组差集算法
parent
d7645d43
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
102 deletions
+26
-102
HttpUtil.go
httpclient/HttpUtil.go
+0
-99
ArrayUtil_test.go
util/ArrayUtil_test.go
+10
-0
HttpUtil.go
util/HttpUtil.go
+16
-3
No files found.
httpclient/HttpUtil.go
deleted
100644 → 0
View file @
d7645d43
package
httpclient
import
(
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// HttpClient 本项目中使用的http客户端
var
HttpClient
*
http
.
Client
func
init
()
{
HttpClient
=
&
http
.
Client
{}
//HttpClient.Timeout = config.Conf.Http.Client.Timeout * time.Millisecond //设置请求超时时间
}
// Get get方法的http请求,返回原始字符串
func
Get
(
url
string
)
([]
byte
,
error
)
{
rep
,
err
:=
HttpClient
.
Get
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
defer
rep
.
Body
.
Close
()
data
,
err
:=
ioutil
.
ReadAll
(
rep
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
// GetWithHeader get方法的http请求,返回原始字符串
func
GetWithHeader
(
url
string
,
header
*
map
[
string
]
string
)
([]
byte
,
error
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
//设置header
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=UTF-8"
)
if
header
!=
nil
{
for
k
,
v
:=
range
*
header
{
req
.
Header
.
Set
(
k
,
v
)
}
}
//发送请求
rep
,
err
:=
HttpClient
.
Do
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
defer
rep
.
Body
.
Close
()
//处理返回值
data
,
err
:=
ioutil
.
ReadAll
(
rep
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
// Post post请求,json
// url 请求地址
// header header参数
// badyParam body参数
func
Post
(
url
string
,
header
*
map
[
string
]
string
,
badyParam
*
interface
{})
([]
byte
,
error
)
{
//处理body参数
var
bt
[]
byte
if
badyParam
!=
nil
{
b
,
err
:=
json
.
Marshal
(
*
badyParam
)
if
err
!=
nil
{
return
nil
,
err
}
bt
=
b
}
//创建post请求
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
bytes
.
NewReader
(
bt
))
if
err
!=
nil
{
return
nil
,
err
}
//设置header
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=UTF-8"
)
if
header
!=
nil
{
for
k
,
v
:=
range
*
header
{
req
.
Header
.
Set
(
k
,
v
)
}
}
//发送请求
rep
,
err
:=
HttpClient
.
Do
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
defer
rep
.
Body
.
Close
()
//处理返回值
data
,
err
:=
ioutil
.
ReadAll
(
rep
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
util/ArrayUtil_test.go
View file @
179a4ecc
...
...
@@ -31,3 +31,13 @@ func TestPostJSONStr(t *testing.T) {
}
fmt
.
Printf
(
"请求结果:%s
\n
"
,
string
(
responseData
))
}
func
TestGet
(
t
*
testing
.
T
)
{
responseData
,
err
:=
Get
(
"https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info?id=1"
,
http
.
Client
{
Timeout
:
10
*
time
.
Second
,
})
if
err
!=
nil
{
fmt
.
Printf
(
"错误:%s
\n
"
,
err
.
Error
())
}
fmt
.
Printf
(
"请求结果:%s
\n
"
,
string
(
responseData
))
}
util/HttpUtil.go
View file @
179a4ecc
...
...
@@ -3,7 +3,7 @@ package util
import
(
"bytes"
"github.com/gin-gonic/gin"
"io
/ioutil
"
"io"
"net/http"
"strings"
)
...
...
@@ -21,6 +21,19 @@ const (
XmlForm
=
"application/x-www-form-urlencoded;charset=utf-8"
)
func
Get
(
url
string
,
client
http
.
Client
)
([]
byte
,
error
)
{
rep
,
err
:=
client
.
Get
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
defer
rep
.
Body
.
Close
()
data
,
err
:=
io
.
ReadAll
(
rep
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
func
PostFormStr
(
url
string
,
params
string
,
header
map
[
string
]
string
,
client
http
.
Client
)
([]
byte
,
error
)
{
p
:=
StrToUrlValue
(
params
)
requestStr
:=
p
.
Encode
()
...
...
@@ -41,7 +54,7 @@ func PostFormStr(url string, params string, header map[string]string, client htt
return
nil
,
err
}
defer
response
.
Body
.
Close
()
data
,
err
:=
io
util
.
ReadAll
(
response
.
Body
)
data
,
err
:=
io
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -66,7 +79,7 @@ func PostJSONStr(url string, params string, header map[string]string, client htt
return
nil
,
err
}
defer
response
.
Body
.
Close
()
data
,
err
:=
io
util
.
ReadAll
(
response
.
Body
)
data
,
err
:=
io
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment