| | |
| | | try { |
| | | |
| | | StringEntity s = new StringEntity(json.toString(), "utf-8"); |
| | | s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, |
| | | "application/json")); |
| | | s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); |
| | | post.setEntity(s); |
| | | |
| | | // 发送请求 |
| | |
| | | |
| | | // 获取响应输入流 |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader( |
| | | inStream, "utf-8")); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8")); |
| | | StringBuilder strber = new StringBuilder(); |
| | | String line = null; |
| | | while ((line = reader.readLine()) != null) |
| | | strber.append(line + "\n"); |
| | | while ((line = reader.readLine()) != null) strber.append(line + "\n"); |
| | | inStream.close(); |
| | | |
| | | result = strber.toString(); |
| | |
| | | } catch (Exception e) { |
| | | System.out.println("请求异常"); |
| | | throw new RuntimeException(e); |
| | | } |
| | | finally { |
| | | } finally { |
| | | //client. |
| | | } |
| | | return result; |
| | |
| | | |
| | | public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception { |
| | | String BOUNDARY = java.util.UUID.randomUUID().toString(); |
| | | MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset()); |
| | | MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.forName("UTF-8")); |
| | | multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile)); //lyricsUpload文件名 |
| | | HttpPost request = new HttpPost(urlStr); //远程接口 |
| | | request.setEntity(multipartEntity); |
| | | request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY); |
| | | request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY + ";charset=UTF-8"); |
| | | |
| | | DefaultHttpClient httpClient = new DefaultHttpClient(); |
| | | HttpResponse response = httpClient.execute(request); |
| | |
| | | while ((line = in.readLine()) != null) { |
| | | buffer.append(line); |
| | | } |
| | | //Flogger.info("发送消息收到的返回:" + buffer.toString()); |
| | | String remark = ""; |
| | | return buffer.toString(); |
| | | // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString()); |
| | | return new String(buffer.toString().getBytes(), "UTF-8"); |
| | | } |
| | | public static String postOpr(String url,String json) |
| | | { |
| | | |
| | | // public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception { |
| | | // String BOUNDARY = java.util.UUID.randomUUID().toString(); |
| | | // MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset()); |
| | | // multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile)); //lyricsUpload文件名 |
| | | // HttpPost request = new HttpPost(urlStr); //远程接口 |
| | | // request.setEntity(multipartEntity); |
| | | // request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY); |
| | | // |
| | | // DefaultHttpClient httpClient = new DefaultHttpClient(); |
| | | // HttpResponse response = httpClient.execute(request); |
| | | // InputStream is = response.getEntity().getContent(); |
| | | // BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
| | | // StringBuffer buffer = new StringBuffer(); |
| | | // String line = ""; |
| | | // while ((line = in.readLine()) != null) { |
| | | // buffer.append(line); |
| | | // } |
| | | // //Flogger.info("发送消息收到的返回:" + buffer.toString()); |
| | | // String remark = ""; |
| | | // return buffer.toString(); |
| | | // // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString()); |
| | | // } |
| | | public static String postOpr(String url, String json) { |
| | | //HttpClient httpClient = new HttpClient(); |
| | | org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); |
| | | PostMethod httpPost = new PostMethod(url); |
| | | httpPost.setRequestHeader("Content-Type","application/json;charset=utf-8"); |
| | | httpPost.setRequestHeader("Content-Type", "application/json;charset=utf-8"); |
| | | |
| | | try |
| | | { |
| | | RequestEntity entity = new StringRequestEntity(json,"Content-Type","UTF-8"); |
| | | try { |
| | | RequestEntity entity = new StringRequestEntity(json, "Content-Type", "UTF-8"); |
| | | httpPost.setRequestEntity(entity); |
| | | httpClient.executeMethod(httpPost); |
| | | |
| | | return httpPost.getResponseBodyAsString(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return httpPost.getResponseBodyAsString(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return ""; |
| | | return ""; |
| | | } |
| | | |
| | | public static String postMsg(String url, JSONObject json) { |
| | | |
| | | CloseableHttpClient client = HttpClients.createDefault(); |
| | |
| | | try { |
| | | |
| | | StringEntity s = new StringEntity(json.toJSONString(), "utf-8"); |
| | | s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, |
| | | "application/json")); |
| | | s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); |
| | | post.setEntity(s); |
| | | |
| | | // 发送请求 |
| | |
| | | |
| | | // 获取响应输入流 |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader( |
| | | inStream, "utf-8")); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8")); |
| | | StringBuilder strber = new StringBuilder(); |
| | | String line = null; |
| | | while ((line = reader.readLine()) != null) |
| | | strber.append(line + "\n"); |
| | | while ((line = reader.readLine()) != null) strber.append(line + "\n"); |
| | | inStream.close(); |
| | | |
| | | result = strber.toString(); |
| | |
| | | } catch (Exception e) { |
| | | System.out.println("请求异常"); |
| | | throw new RuntimeException(e); |
| | | } |
| | | finally { |
| | | } finally { |
| | | //client. |
| | | } |
| | | return result; |