Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
thrunder liu
httpClient
提交
0f59b08d
H
httpClient
项目概览
thrunder liu
/
httpClient
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
httpClient
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0f59b08d
编写于
1月 31, 2019
作者:
L
liu_lei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
提交测试成功工程
上级
2789a930
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
339 addition
and
0 deletion
+339
-0
pom.xml
pom.xml
+106
-0
src/main/java/com/liulei/fly/MyHttpClient.java
src/main/java/com/liulei/fly/MyHttpClient.java
+145
-0
src/main/java/com/liulei/fly/Test.java
src/main/java/com/liulei/fly/Test.java
+68
-0
src/test/java/com/liulei/fly/AppTest.java
src/test/java/com/liulei/fly/AppTest.java
+20
-0
未找到文件。
pom.xml
0 → 100644
浏览文件 @
0f59b08d
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.liulei.fly
</groupId>
<artifactId>
HttpClient
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<name>
HttpClient
</name>
<!-- FIXME change it to the project's website -->
<url>
http://www.example.com
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>
1.7
</maven.compiler.source>
<maven.compiler.target>
1.7
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.11
</version>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>
com.telek.commons-httpclient
</groupId>
<artifactId>
commons-httpclient
</artifactId>
<version>
3.1
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.8.1
</version>
</dependency>
<dependency>
<groupId>
com.sgcc
</groupId>
<artifactId>
SGCCPlatform-business-serializeUtil
</artifactId>
<version>
1.1
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
<version>
1.2
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.6
</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>
maven-clean-plugin
</artifactId>
<version>
3.1.0
</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>
maven-resources-plugin
</artifactId>
<version>
3.0.2
</version>
</plugin>
<plugin>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.8.0
</version>
</plugin>
<plugin>
<artifactId>
maven-surefire-plugin
</artifactId>
<version>
2.22.1
</version>
</plugin>
<plugin>
<artifactId>
maven-jar-plugin
</artifactId>
<version>
3.0.2
</version>
</plugin>
<plugin>
<artifactId>
maven-install-plugin
</artifactId>
<version>
2.5.2
</version>
</plugin>
<plugin>
<artifactId>
maven-deploy-plugin
</artifactId>
<version>
2.8.2
</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>
maven-site-plugin
</artifactId>
<version>
3.7.1
</version>
</plugin>
<plugin>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
<version>
3.0.0
</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
src/main/java/com/liulei/fly/MyHttpClient.java
0 → 100644
浏览文件 @
0f59b08d
package
com.liulei.fly
;
import
org.apache.commons.httpclient.*
;
import
org.apache.commons.httpclient.cookie.CookiePolicy
;
import
org.apache.commons.httpclient.methods.ByteArrayRequestEntity
;
import
org.apache.commons.httpclient.methods.GetMethod
;
import
org.apache.commons.httpclient.methods.PostMethod
;
import
org.apache.commons.httpclient.methods.StringRequestEntity
;
import
org.apache.commons.httpclient.params.HttpMethodParams
;
import
org.apache.commons.httpclient.util.URIUtil
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.ByteArrayEntity
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author liu_l
* @email: liu_lei_programmer@163.com
* @time 2019/1/21 16:43
* @Description: 描述:
*/
public
class
MyHttpClient
{
private
HttpClient
httpClient
;
private
String
cookies
;
public
MyHttpClient
(
String
ip
,
int
port
)
{
httpClient
=
new
HttpClient
();
httpClient
.
getHostConfiguration
().
setHost
(
ip
,
port
,
"http"
);
httpClient
.
getParams
().
setCookiePolicy
(
CookiePolicy
.
BROWSER_COMPATIBILITY
);
}
public
String
getCookies
(){
return
cookies
;
}
public
void
setCookies
(
String
cookies
){
this
.
cookies
=
cookies
;
}
public
String
doGet
(
String
url
)
throws
IOException
{
return
doGet
(
url
,
null
);
}
public
String
doGet
(
String
url
,
String
paras
)
throws
IOException
{
GetMethod
method
=
new
GetMethod
(
url
);
if
(
StringUtils
.
isNotBlank
(
paras
))
{
method
.
setQueryString
(
URIUtil
.
encodeQuery
(
paras
));
}
return
sendRequest
(
method
);
}
public
String
doPost
(
String
url
,
Map
<
String
,
String
>
paras
)
throws
IOException
{
int
index
=
0
;
NameValuePair
[]
data
=
new
NameValuePair
[
paras
.
size
()];
for
(
Map
.
Entry
<
String
,
String
>
entry:
paras
.
entrySet
()){
data
[
index
++]
=
new
NameValuePair
(
entry
.
getKey
(),
entry
.
getValue
());
}
return
doPost
(
url
,
data
);
}
private
String
doPost
(
String
url
,
NameValuePair
[]
data
)
throws
IOException
{
PostMethod
method
=
new
PostMethod
(
url
);
method
.
setRequestHeader
(
"loginCompcode"
,
"41311016120100001"
);
method
.
setRequestHeader
(
"compcode"
,
"41311016120100001"
);
method
.
setRequestHeader
(
"userName"
,
"41311016120100001"
);
method
.
setRequestBody
(
data
);
method
.
getParams
().
setParameter
(
HttpMethodParams
.
HTTP_CONTENT_CHARSET
,
"utf-8"
);
return
sendRequest
(
method
);
}
private
String
sendRequest
(
HttpMethod
method
)
throws
IOException
{
StringBuffer
response
=
new
StringBuffer
();
try
{
method
.
setRequestHeader
(
"token"
,
cookies
);
httpClient
.
executeMethod
(
method
);
if
(
method
.
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
response
.
append
(
getResponseBody
(
method
));
}
else
if
(
method
.
getStatusCode
()
==
HttpStatus
.
SC_MOVED_TEMPORARILY
){
//302 重定向
response
.
append
(
getResponseHeader
(
method
));
}
else
{
System
.
out
.
println
(
"response statuscode : "
+
method
.
getStatusCode
());
throw
new
IllegalStateException
(
"response statuscode : "
+
method
.
getStatusCode
());
}
fetchCookies
();
}
finally
{
method
.
releaseConnection
();
}
return
response
.
toString
();
}
private
void
fetchCookies
(){
Cookie
[]
values
=
httpClient
.
getState
().
getCookies
();
StringBuffer
cookieBuffer
=
new
StringBuffer
();
for
(
Cookie
c
:
values
)
{
cookieBuffer
.
append
(
c
.
toString
()).
append
(
";"
);
}
cookies
=
cookieBuffer
.
toString
();
}
private
String
getResponseBody
(
HttpMethod
method
)
throws
IOException
,
IOException
{
StringBuffer
response
=
new
StringBuffer
();
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
method
.
getResponseBodyAsStream
(),
"UTF-8"
));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
response
.
append
(
line
).
append
(
System
.
getProperty
(
"line.separator"
));
}
}
finally
{
if
(
reader
!=
null
)
{
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
}
}
}
return
response
.
toString
();
}
private
String
getResponseHeader
(
HttpMethod
method
){
StringBuffer
response
=
new
StringBuffer
();
Header
[]
headers
=
method
.
getResponseHeaders
();
for
(
Header
header:
headers
){
String
line
=
header
.
getName
()+
" : "
+
header
.
getValue
();
response
.
append
(
line
);
response
.
append
(
System
.
getProperty
(
"line.separator"
));
}
return
response
.
toString
();
}
}
src/main/java/com/liulei/fly/Test.java
0 → 100644
浏览文件 @
0f59b08d
package
com.liulei.fly
;
import
com.nimbusds.jose.JOSEException
;
import
com.telek.business.jwtUtil.JwtUtil
;
import
com.telek.business.message.Param
;
import
com.telek.business.protoUtil.SerializeUtil
;
import
net.minidev.json.JSONObject
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author liu_l
* @email: liu_lei_programmer@163.com
* @time 2019/1/21 17:00
* @Description: 描述: 发送http请求测试类
*/
public
class
Test
{
private
static
final
Log
log
=
LogFactory
.
getLog
(
Test
.
class
);
private
static
final
String
charssetName
=
"ISO-8859-1"
;
public
static
void
main
(
String
[]
args
)
throws
IOException
{
MyHttpClient
myHttpClient
=
new
MyHttpClient
(
"192.168.1.96"
,
3333
);
try
{
myHttpClient
.
setCookies
(
getCookie
());
}
catch
(
JOSEException
e
)
{
log
.
error
(
"创建cookie失败"
);
e
.
printStackTrace
();
}
Map
<
String
,
String
>
paras
=
new
HashMap
<
String
,
String
>();
String
param
=
getParam
();
paras
.
put
(
"param"
,
param
);
String
url
=
"/sanlv/getContructionItem"
;
String
result
=
myHttpClient
.
doPost
(
url
,
paras
);
System
.
out
.
println
(
result
);
}
private
static
String
getCookie
()
throws
JOSEException
{
JSONObject
payload
=
new
JSONObject
();
Date
date
=
new
Date
();
payload
.
put
(
"cookie"
,
"291969452"
);
//用户id
payload
.
put
(
"userName"
,
"41311016120100001"
);
//登录账号
payload
.
put
(
"loginCompcode"
,
"41311016120100001"
);
//查询单位
payload
.
put
(
"compcode"
,
"41311016120100001"
);
//查询单位
payload
.
put
(
"ext"
,
date
.
getTime
()+
1000
*
60
*
60
);
//过期时间1小时
String
token
=
JwtUtil
.
createToken
(
payload
);
return
token
;
}
private
static
String
getParam
()
throws
UnsupportedEncodingException
{
SerializeUtil
.
initProtoParam
(
false
);
Param
param
=
new
Param
();
//param.setLoginCompcode("41311016120100001");
//param.setCompcode("41311016120100001");
param
.
setItemCode
(
"asdf"
);
param
.
setItemName
(
"我是你爸爸"
);
byte
[]
bytes
=
SerializeUtil
.
serialize
(
param
);
String
bytes2String
=
new
String
(
bytes
,
charssetName
);
return
bytes2String
;
}
}
src/test/java/com/liulei/fly/AppTest.java
0 → 100644
浏览文件 @
0f59b08d
package
com.liulei.fly
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.junit.Test
;
/**
* Unit test for simple App.
*/
public
class
AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public
void
shouldAnswerWithTrue
()
{
assertTrue
(
true
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录