HttpClient를 시뮬레이션 HTTP 요청

좌우의 pom.xml 도입

 

<의존성> 
            <의 groupId> org.apache.httpcomponents </의 groupId> 
            <artifactId를> HttpClient를 </ artifactId를> 
        </ 의존성> 
        <의존성> 
            <의 groupId> org.apache.httpcomponents </의 groupId> 
            <artifactId를> httpmime </ artifactId를> 
        </ 의존성> 
        <의존성> 
            <의 groupId> org.apache.httpcomponents </의 groupId> 
            <artifactId를> HttpClient를-OSGI </ artifactId를> 
        </ 의존성>

 

ApiConsts.java

공용  인터페이스 ApiConsts { 

    INT ApiTimeout = 10000 ; 
    문자셋 ApiCharset = Charset.defaultCharset (); 
    문자열 ApiCharsetName = ApiCharset.name (); 
    문자열 DateTimeFormat = "YYYY-MM-DD HH : MM : SS" ; 

}

HttpUtils.java

수입 org.apache.http.Header;
수입 org.apache.http.client.ClientProtocolException;
수입 org.apache.http.client.fluent.Request;
수입 org.apache.http.entity.StringEntity;
수입 org.apache.http.message.BasicHeader;
수입 org.springframework.stereotype.Component;
/ ** 
 * 
 * @author wanjun 
 * 
 * / 
@Component 
공공  클래스 HttpUtils { 

    개인  정적 헤더 [] DefaultJSONHeader = 새로운 헤더 [] {
             새로운 BasicHeader ( "Content-Type을", "응용 프로그램 / JSON; 문자셋 ="+ApiConsts.ApiCharsetName)}; 

    개인  정적 헤더는 [] getDefaultJSONHeaderWithCookie은 (문자열 cookcie가) {
         반환 DefaultJSONHeader = 새로운 헤더 [] {
                 새로운 BasicHeader ( "Content-Type을", "응용 프로그램 / JSON; 문자셋 ="+ ApiConsts.ApiCharsetName),
                 새로운 BasicHeader ( "쿠키"를 , cookcie )}; 
    }; 

    공공 문자열의 doPost (문자열 apiUrl, 문자열 requestBody, 문자열 쿠키) 가 발생 예외 {
         시도 {
             INT 시간 제한 = ApiConsts.ApiTimeout; 
            에서 System.out.println ("============ [HTTP POST] ================" ); 
            에서 System.out.println ( "URL :"+ apiUrl + ", 제한 시간 :"+ 타임 아웃 + "MS" ); 
            에서 System.out.println ( "요청 본문 :"+ requestBody); 
            문자열 응답 = Request.Post (apiUrl) .socketTimeout (시간 초과) .connectTimeout (시간 초과) 
                    .body ( StringEntity (requestBody, ApiConsts.ApiCharset)) 
                    .setHeaders (getDefaultJSONHeaderWithCookie (쿠키)). (실행). returnContent () 
                    .asString (ApiConsts.ApiCharset); 

            에서 System.out.println ( "응답 바디 :"+ 답변 + "\ n"반환 대답을; 
        } 캐치 (ClientProtocolException 전자) { 
            e.printStackTrace (); 
        } 
        반환 requestBody을; 
    } 

    공공 문자열의 doGet (문자열 apiUrl)가 발생 예외 {
         시도 {
             INT 시간 제한 = ApiConsts.ApiTimeout; 
            에서 System.out.println ( "=============== [HTTP GET] ================" ); 
            에서 System.out.println ( "URL :"+ apiUrl + ", 제한 시간 :"+ 타임 아웃 + "MS" ); 

            문자열 응답 =Request.Get (apiUrl) .socketTimeout (초과) .connectTimeout (초과) .Execute를 () returnContent (). 
                    .asString (ApiConsts.ApiCharset); 

            에서 System.out.println ( "응답 바디 :"+ 답변 + "\ n" );
            반환 대답을; 

        } 캐치 (ClientProtocolException 전자) { 
            e.printStackTrace (); 
        } 
        반환 apiUrl을; 
    } 

}

테스트 :

@RequestMapping ( "/ getMoney" )
     공공 문자열 memberWithdrawal은 (문자열 사용자 이름, 문자열 PWD가, 문자열 돈이) 던져 예외 { 
        사용자 정보 사용자 정보 = userInfoMapper.queryUserInfoByUserName (사용자 이름); 
        RequestParamBean PARAM = newRequestParamBean (); 
        // param.set ()
         // param.set () 
     문자열 반응 = httpUtils.doPost ( "URL", JSON.toJSONString (getMoney), userInfo.getCookie ()); 반환 응답; }       

 

추천

출처www.cnblogs.com/wanjun-top/p/12625798.html