Springboot+httpリクエスト変換


Springboot+httpリクエスト変換
##この記事では、プロジェクトがウィジェットとpcが互いに同じビジネスロジックを呼び出すインタフェースを必要とすることを記録します.この方法では、レポートにターゲットurlを追加するように要求する必要があります.他の余分なコード開発はありません.
httpリクエスト
後でhttp 1を専門に勉強します.メソッド+url+プロトコル/バージョン2.要求ヘッダ3.トピックspringboot+http请求转换_第1张图片
springboot
Pom.xml  
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.0.RELEASE</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.44</version>
  </dependency>

@SpringBootApplication
public class Application {
     
    public static void main(String[] args) {
     
        SpringApplication.run(Application.class,args);
    }
}
Controller---
@RequestMapping("/adapter")
    @ApiOperation(value = "", notes = "      ")
    public RestMessage adapter(@RequestBody String requestParameter) {
     
        // Controller      (    )
        Date startDate = new Date();
        String sdfsd=AESUtilTools.encrypt(requestParameter);
        String requestJsonEnc = AESUtilTools.decrypt(sdfsd);//  
        JSONObject  requestJson = JSONObject.parseObject(requestJsonEnc);
        RestMessage restMessage = addattendanceruleServiceImpl.Adapter(requestJsonEnc);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return restMessage;
    }


public static String httpSend(String requestUrl,String requestXML ) throws Exception {
     
        String msgXML = "";
        HttpURLConnection httpConnection = null;
        try {
     
            URL url = new URL(requestUrl);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setConnectTimeout(500);
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            httpConnection.setAllowUserInteraction(true);
            httpConnection.setRequestProperty("Content-type","application/json");
           
            httpConnection.setRequestProperty("accept", "*/*");
            httpConnection.setRequestProperty("connection", "Keep-Alive");
            httpConnection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1;SV1)");
            String user = SysConst.getProperty("User");
            String passwordkey =SysConst.getProperty("password");
            BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    		//   ,          
    		textEncryptor.setPassword("password");
    		//   ,          
    		String pwd = textEncryptor.decrypt(passwordkey);
            String auth = user+":"+pwd;
            byte[] rel = Base64.encodeBase64(auth.getBytes());
            String res = new String(rel);
  //       
           httpConnection.setRequestProperty("Authorization","Basic " + res);
//            httpConnection.setReadTimeout(readTimeout);
            httpConnection.connect();
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(requestXML.getBytes("utf-8"));
            outputStream.flush();
            outputStream.close();
            // 3、    
            InputStreamReader inputStreamReader = new InputStreamReader(
                    httpConnection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);

            String inputLine = "";
            StringBuffer inputLines = new StringBuffer();
            inputLine = bufferedReader.readLine();
            while (inputLine != null) {
     
                inputLines.append(inputLine);
                inputLine = bufferedReader.readLine();
            }
            inputStreamReader.close();
            bufferedReader.close();
            // 4、    
            httpConnection.disconnect();
            msgXML = inputLines.toString();
            return msgXML;
        } catch (Exception e) {
     
            e.printStackTrace();
            throw e;
        } finally {
     
            if (httpConnection != null) {
     
                httpConnection.disconnect();
            }
        }
    }

   

public static String callByHttpPost(String url , String json) throws ExecutionException{
     
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
		String responseData = "";
		try {
     
			postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			postMethod.setRequestHeader("Content-Type","application/json");
			StringRequestEntity requestEntity = new StringRequestEntity(json, "application/json", "UTF-8");
			postMethod.setRequestEntity(requestEntity);
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode == HttpStatus.SC_OK) {
     
				responseData =  postMethod.getResponseBodyAsString();
			}
		} catch (Exception e) {
     
			Logger logger = Logger.getLogger(e.toString());
			throw new ExecutionException(e);
		}finally{
     
			postMethod.releaseConnection();
		}
		return responseData;
	}