httpclientリクエストパラメータの取得方法
16286 ワード
文書ディレクトリ
目的
別のブログコードのニーズを参照してください.https://blog.csdn.net/GY325416/article/details/81412078
フロントgetリクエストのパラメータの取得方法 /**
* get
*
* @param uri
* @param request controller
* @author piper
* @data 2018/7/3 11:19
*/
HttpGet getMethod(String uri, HttpServletRequest request) {
try {
URIBuilder builder = new URIBuilder(uri);
Enumeration<String> enumeration = request.getParameterNames();
//
while (enumeration.hasMoreElements()) {
String nex = enumeration.nextElement();
builder.setParameter(nex, request.getParameter(nex));
}
return new HttpGet(builder.build());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
postリクエストのパラメータの取得方法
postリクエスト時にformフォームまたはjsonデータが送信される可能性があります
フロントがjsonデータを転送しているとどう判断しますか?requestオブジェクトのContentTypeリクエストヘッダによりjsonであればContent-Typeはアプリケーション-jsonであるべきであると判断できる.charset=utf-8なので、リクエストヘッダ判定を取得すればよい/**
* post
*
* @param uri
* @param request
* @author piper
* @data 2018/7/3 11:19
*/
HttpPost postMethod(String uri, HttpServletRequest request) {
StringEntity entity = null;
if (request.getContentType().contains("json")) {
entity = jsonData(request); // json
} else {
entity = formData(request); // form
}
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Content-Type", request.getHeader("Content-Type"));
httpPost.setEntity(entity);
return httpPost;
}
postリクエストのformフォームデータの取得
/**
* post form form
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public UrlEncodedFormEntity formData(HttpServletRequest request) {
UrlEncodedFormEntity urlEncodedFormEntity = null;
try {
List<NameValuePair> pairs = new ArrayList<>(); //
Enumeration<String> params = request.getParameterNames(); //
while (params.hasMoreElements()) {
String name = params.nextElement();
pairs.add(new BasicNameValuePair(name, request.getParameter(name)));
}
// , post
urlEncodedFormEntity = new UrlEncodedFormEntity(pairs, request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlEncodedFormEntity;
}
postリクエストのjsonデータの取得
/**
* post json
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public StringEntity jsonData(HttpServletRequest request) {
InputStreamReader is = null;
try {
is = new InputStreamReader(request.getInputStream(), request.getCharacterEncoding());
BufferedReader reader = new BufferedReader(is);
// json String
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
// json
return new StringEntity(sb.toString(), request.getCharacterEncoding());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* get
*
* @param uri
* @param request controller
* @author piper
* @data 2018/7/3 11:19
*/
HttpGet getMethod(String uri, HttpServletRequest request) {
try {
URIBuilder builder = new URIBuilder(uri);
Enumeration<String> enumeration = request.getParameterNames();
//
while (enumeration.hasMoreElements()) {
String nex = enumeration.nextElement();
builder.setParameter(nex, request.getParameter(nex));
}
return new HttpGet(builder.build());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
postリクエストのパラメータの取得方法
postリクエスト時にformフォームまたはjsonデータが送信される可能性があります
フロントがjsonデータを転送しているとどう判断しますか?requestオブジェクトのContentTypeリクエストヘッダによりjsonであればContent-Typeはアプリケーション-jsonであるべきであると判断できる.charset=utf-8なので、リクエストヘッダ判定を取得すればよい/**
* post
*
* @param uri
* @param request
* @author piper
* @data 2018/7/3 11:19
*/
HttpPost postMethod(String uri, HttpServletRequest request) {
StringEntity entity = null;
if (request.getContentType().contains("json")) {
entity = jsonData(request); // json
} else {
entity = formData(request); // form
}
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Content-Type", request.getHeader("Content-Type"));
httpPost.setEntity(entity);
return httpPost;
}
postリクエストのformフォームデータの取得
/**
* post form form
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public UrlEncodedFormEntity formData(HttpServletRequest request) {
UrlEncodedFormEntity urlEncodedFormEntity = null;
try {
List<NameValuePair> pairs = new ArrayList<>(); //
Enumeration<String> params = request.getParameterNames(); //
while (params.hasMoreElements()) {
String name = params.nextElement();
pairs.add(new BasicNameValuePair(name, request.getParameter(name)));
}
// , post
urlEncodedFormEntity = new UrlEncodedFormEntity(pairs, request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlEncodedFormEntity;
}
postリクエストのjsonデータの取得
/**
* post json
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public StringEntity jsonData(HttpServletRequest request) {
InputStreamReader is = null;
try {
is = new InputStreamReader(request.getInputStream(), request.getCharacterEncoding());
BufferedReader reader = new BufferedReader(is);
// json String
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
// json
return new StringEntity(sb.toString(), request.getCharacterEncoding());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post
*
* @param uri
* @param request
* @author piper
* @data 2018/7/3 11:19
*/
HttpPost postMethod(String uri, HttpServletRequest request) {
StringEntity entity = null;
if (request.getContentType().contains("json")) {
entity = jsonData(request); // json
} else {
entity = formData(request); // form
}
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Content-Type", request.getHeader("Content-Type"));
httpPost.setEntity(entity);
return httpPost;
}
/**
* post form form
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public UrlEncodedFormEntity formData(HttpServletRequest request) {
UrlEncodedFormEntity urlEncodedFormEntity = null;
try {
List<NameValuePair> pairs = new ArrayList<>(); //
Enumeration<String> params = request.getParameterNames(); //
while (params.hasMoreElements()) {
String name = params.nextElement();
pairs.add(new BasicNameValuePair(name, request.getParameter(name)));
}
// , post
urlEncodedFormEntity = new UrlEncodedFormEntity(pairs, request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlEncodedFormEntity;
}
/**
* post json
*
* @param request
* @author piper
* @data 2018/7/17 18:05
*/
public StringEntity jsonData(HttpServletRequest request) {
InputStreamReader is = null;
try {
is = new InputStreamReader(request.getInputStream(), request.getCharacterEncoding());
BufferedReader reader = new BufferedReader(is);
// json String
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
// json
return new StringEntity(sb.toString(), request.getCharacterEncoding());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}