자바 웹 http 전송 요청
/***
* Get request query string
* @param request
* @return byte[]
*/
public byte[] getRequestStr(HttpServletRequest request){
int contentLength = request.getContentLength();
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;) {
try {
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
} catch (IOException ioexception) {
ioexception.printStackTrace();
} finally {
// logger.info("Json Request:" + requestPacket);
}
}
return buffer;
}
상술 한 방법 은 byte 배열 을 되 돌려 줍 니 다.
다음 방법 은 문자열 을 직접 되 돌려 줍 니 다:
/***
* Get request query string
*
* @param request
* @return
* @throws UnsupportedEncodingException
*/
public String getRequestStr(HttpServletRequest request) throws UnsupportedEncodingException{
byte buffer[]=getRequestBytes(request);
String charEncoding=request.getCharacterEncoding();
if(charEncoding==null){
charEncoding="UTF-8";
}
return new String(buffer,charEncoding);
}
활용 단어 참조
:상기 방법 은 일반적으로 filter(javax.servlet.Filter)에서 요청 파 라 메 터 를 가 져 와 전송 하 는 데 사 용 됩 니 다.
자바 웹 에서 response 응답 체(응답 체)를 다시 쓰 고 응답 체 에 지정 한 내용 을 어떻게 기록 합 니까?
/***
* Send http request
*
* @param response
* @param bytes :
* @param contentType :if is null,default value is "application/json"
* @param encoding :
* @throws IOException
*/
public static void sendRequestWriter(HttpServletResponse response, byte[] bytes,
String contentType,String encoding) throws IOException {
response.setContentLength(bytes.length);
if (contentType == null) {
contentType = "application/json";
}
response.setContentType(contentType);
PrintWriter printer = response.getWriter();
printer.println(new String(bytes,encoding));
printer.flush();
printer.close();
}
/***
*
* @param response
* @param sendData :<code>String</code>
* @param contentType
* @param encoding : such as GBK/utf-8
* @throws IOException
*/
public static void sendRequestWriter(HttpServletResponse response, String sendData,
String contentType,String encoding) throws IOException {
// response.setContentLength(sendData.getBytes(encoding).length);
byte[]bytes=sendData.getBytes(encoding);
sendRequestWriter(response, bytes, contentType, encoding);
}
이상 의 방법 은 모두 PrintWriter 를 사용 하여 response 를 기록 합 니 다.
다음 방식 은 스 트림 으로 response 를 기록 하 는 것 입 니 다.
/***
* test ok
* @param response
* @param bytes
* @param contentType
* @param encoding
* @throws IOException
*/
public static void sendRequestStream(HttpServletResponse response, byte[] bytes,
String contentType) throws IOException {
response.setContentLength(bytes.length);
if (contentType == null) {
contentType = "application/json";
}
response.setContentType(contentType);
ServletOutputStream sos = response.getOutputStream();
sos.write(bytes, 0, bytes.length);
sos.flush();
sos.close();
}
응용:게 이 트 웨 이 에서 전송 요청 과 응답 을 하 는 데 사용 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
조건자로 필터링프로그래밍에서 컬렉션이 있을 때 요구 사항을 충족하는 부분을 필터링하고 나머지는 버려야 하는 경우가 있습니다. 자세한 예는 환자 기록 목록이 있고 미성년 환자 목록을 가져오려는 경우입니다. 이 코드를 자바 파일에 복...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.