import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpClientUtil { /** * 发送 get请求 * 参考博客 * @throws IOException * @throws ClientProtocolException */ public static String get(String url) throws ClientProtocolException, IOException, ParseException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpGet httpget = new HttpGet(url); //httpget.addHeader("Accept-Language:zh-CN", "zh;q=0.8"); //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httpget.setConfig(defaultRequestConfig); System.out.println("executing request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 return EntityUtils.toString(entity, "utf-8"); } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String get(String url,int retry) throws ClientProtocolException, IOException, ParseException{ Exception exp = null; for (int i = 0; i < retry; i++) { try { return get(url); } catch (ClientProtocolException e) { exp = e; } catch (IOException e) { exp = e; } catch (ParseException e) { exp = e; } } if(exp instanceof ClientProtocolException){ ClientProtocolException t = (ClientProtocolException) exp; throw t; }else if(exp instanceof IOException){ IOException t = (IOException) exp; throw t; }else{ ParseException t = (ParseException) exp; throw t; } } public static String get(Map<String,String> header,String url,int retry) throws ClientProtocolException, IOException, ParseException{ Exception exp = null; for (int i = 0; i < retry; i++) { try { return get(header,url); } catch (ClientProtocolException e) { exp = e; } catch (IOException e) { exp = e; } catch (ParseException e) { exp = e; } } if(exp instanceof ClientProtocolException){ ClientProtocolException t = (ClientProtocolException) exp; throw t; }else if(exp instanceof IOException){ IOException t = (IOException) exp; throw t; }else{ ParseException t = (ParseException) exp; throw t; } } public static String get(Map<String,String> header,String url) throws ClientProtocolException, IOException, ParseException{ CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpGet httpget = new HttpGet(url); if(header != null){ for(Entry<String, String> param : header.entrySet()){ httpget.addHeader(param.getKey(), param.getValue()); } } //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httpget.setConfig(defaultRequestConfig); System.out.println("executing request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 return EntityUtils.toString(entity, "utf-8"); } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static byte[] getBytes(String url,int retry) throws ClientProtocolException, IOException, ParseException{ Exception exp = null; for (int i = 0; i < retry; i++) { try { return getBytes(url); } catch (ClientProtocolException e) { exp = e; } catch (IOException e) { exp = e; } catch (ParseException e) { exp = e; } } if(exp instanceof ClientProtocolException){ ClientProtocolException t = (ClientProtocolException) exp; throw t; }else if(exp instanceof IOException){ IOException t = (IOException) exp; throw t; }else{ ParseException t = (ParseException) exp; throw t; } } public static byte[] getBytes(String url) throws ClientProtocolException, IOException, ParseException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpGet httpget = new HttpGet(url); //httpget.addHeader("Accept-Language:zh-CN", "zh;q=0.8"); //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httpget.setConfig(defaultRequestConfig); System.out.println("executing request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 return EntityUtils.toByteArray(entity); } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String postFileMultiPart(String url,Map<String,ContentBody> reqParam) throws ClientProtocolException, IOException{ return postFileMultiPart(null,url,reqParam); } public static String postFileMultiPart(Map<String, String> header,String url, Map<String, ContentBody> reqParam) throws ClientProtocolException, IOException{ CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpPost httppost = new HttpPost(url); if(header != null){ for(Entry<String, String> param : header.entrySet()){ httppost.addHeader(param.getKey(), param.getValue()); } } //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httppost.setConfig(defaultRequestConfig); System.out.println("executing request " + httppost.getURI()); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); for(Entry<String,ContentBody> param : reqParam.entrySet()){ multipartEntityBuilder.addPart(param.getKey(), param.getValue()); } HttpEntity reqEntity = multipartEntityBuilder.build(); httppost.setEntity(reqEntity); // 执行post请求. CloseableHttpResponse response = httpclient.execute(httppost); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 return EntityUtils.toString(entity,Charset.forName("UTF-8")); } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String post(String url,Map<String,String> reqParam) throws ClientProtocolException, IOException{ CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httppost. HttpPost httppost = new HttpPost(url); //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httppost.setConfig(defaultRequestConfig); System.out.println("executing request " + httppost.getURI()); //装填参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(reqParam !=null ){ for (Entry<String, String> entry : reqParam.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } UrlEncodedFormEntity reqEntity = new UrlEncodedFormEntity(nvps,"utf-8"); httppost.setEntity(reqEntity); // 执行post请求. CloseableHttpResponse response = httpclient.execute(httppost); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 return EntityUtils.toString(entity,Charset.forName("UTF-8")); } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static JSONObject post4cookie(String url,JSONObject reqParam) throws ClientProtocolException, IOException{ CookieStore cookieStore = new BasicCookieStore(); //CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); try { // 创建httppost. HttpPost httppost = new HttpPost(url); //setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build(); httppost.setConfig(defaultRequestConfig); System.out.println("executing request " + httppost.getURI()); //装填参数 StringEntity reqEntity = new StringEntity(reqParam.toString(), Charset.forName("UTF-8")); reqEntity.setContentType("application/json"); httppost.setEntity(reqEntity); // 执行post请求. CloseableHttpResponse response = httpclient.execute(httppost); System.out.println("got response"); try { // 获取响应实体 HttpEntity entity = response.getEntity(); //System.out.println("--------------------------------------"); // 打印响应状态 //System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 //System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 JSONObject res = new JSONObject(); String httpRes = EntityUtils.toString(entity,Charset.forName("UTF-8")); res.put("httpRes", httpRes); JSONObject cookie = new JSONObject(); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { cookie.put(cookies.get(i).getName(), cookies.get(i).getValue()); } res.put("cookie", cookie); return res; } //System.out.println("------------------------------------"); } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpMultipartFormdataDemo1 { public static void main(String[] args) throws Exception { // 文件sTestsetFile:solr_etl_agent35.json是存有JSON字符串的文件 String sTestsetFile="C:/Users/Thinkpad/Desktop/com"; File f = new File(sTestsetFile); sw( f); } public static void upload(File file) throws ClientProtocolException, IOException{ String sURL="http://javare.cn/De"; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse httpResponse = null; RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build(); HttpPost httpPost = new HttpPost(sURL); httpPost.setConfig(requestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); //multipartEntityBuilder.setCharset(Charset.forName("UTF-8")); //File file = new File("F:\\Ken\\1.PNG"); //FileBody bin = new FileBody(file); //multipartEntityBuilder.addBinaryBody("file", file,ContentType.create("image/png"),"abc.pdf"); //当设置了setSocketTimeout参数后,以下代码上传PDF不能成功,将setSocketTimeout参数去掉后此可以上传成功。上传图片则没有个限制 //multipartEntityBuilder.addBinaryBody("file",file,ContentType.create("application/octet-stream"),"abd.pdf"); multipartEntityBuilder.addBinaryBody("file",file); //multipartEntityBuilder.addPart("comment", new StringBody("This is comment", ContentType.TEXT_PLAIN)); multipartEntityBuilder.addTextBody("comment", "this is comment"); HttpEntity httpEntity = multipartEntityBuilder.build(); httpPost.setEntity(httpEntity); httpResponse = httpClient.execute(httpPost); httpClient.close(); if(httpResponse!=null){ httpResponse.close(); } } private static void sw(File f) throws Exception { // TODO Auto-generated method stub String sURL="http://javare.cn/De"; for(int i=0;i<f.listFiles().length;i++){ if(!f.listFiles()[i].isDirectory()){ String url = sURL; String httpRes = null; String localFileName = f.listFiles()[i].getAbsolutePath(); Map<String,ContentBody> reqParam = new HashMap<String,ContentBody>(); reqParam.put("Filename", new StringBody(localFileName, ContentType.MULTIPART_FORM_DATA)); reqParam.put("pictitle", new StringBody(localFileName, ContentType.MULTIPART_FORM_DATA)); reqParam.put("dir", new StringBody("upload1", ContentType.MULTIPART_FORM_DATA)); reqParam.put("fileNameFormat", new StringBody("{time}{rand:6}", ContentType.MULTIPART_FORM_DATA)); reqParam.put("fileName", new StringBody(localFileName, ContentType.MULTIPART_FORM_DATA)); reqParam.put("fileName", new StringBody(localFileName, ContentType.MULTIPART_FORM_DATA)); reqParam.put("upfile", new FileBody(new File(localFileName))); reqParam.put("Upload", new StringBody("Submit Query", ContentType.MULTIPART_FORM_DATA)); httpRes = HttpClientUtil.postFileMultiPart(url,reqParam); System.out.println(httpRes); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String id=httpRes.substring(httpRes.indexOf("'.")+3, httpRes.indexOf("';")); System.out.println(id); getHtml(id,f.listFiles()[i]); // // CloseableHttpClient httpclient = HttpClients.createDefault(); // //CloseableHttpClient httpclient = HttpClientBuilder.create().build(); // try { // HttpPost httppost = new HttpPost(sURL); // // RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build(); // httppost.setConfig(requestConfig); // // FileBody bin = new FileBody(f.listFiles()[i]); // StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN); // HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", comment).build(); // httppost.setEntity(reqEntity); // System.out.println("executing request " + httppost.getRequestLine()); // CloseableHttpResponse response = httpclient.execute(httppost); // try { // System.out.println(response.getStatusLine()); // HttpEntity resEntity = response.getEntity(); // if (resEntity != null) { // String responseEntityStr = EntityUtils.toString(response.getEntity()); // System.out.println(responseEntityStr); // System.out.println("Response content length: " + resEntity.getContentLength()); // String id=responseEntityStr.substring(responseEntityStr.indexOf("'.")+3, responseEntityStr.indexOf("';")); // System.out.println(id); // System.out.println(); // getHtml(id,f.listFiles()[i]); // } // EntityUtils.consume(resEntity); // } finally { // response.close(); // } // } catch (ClientProtocolException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } finally { // try { // httpclient.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // CloseableHttpClient httpClient = HttpClients.createDefault(); // HttpPost uploadFile = new HttpPost(sURL); // MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN); // builder.addBinaryBody( // "file", // new FileInputStream(f.listFiles()[i]), // ContentType.MULTIPART_FORM_DATA, // f.listFiles()[i].getName() // ); // // HttpEntity multipart = builder.build(); // uploadFile.setEntity(multipart); // CloseableHttpResponse response = httpClient.execute(uploadFile); // HttpEntity responseEntity = response.getEntity(); // String sResponse=EntityUtils.toString(responseEntity, "UTF-8"); // System.out.println("Post 返回结果"+sResponse); // httpClient.close(); // if(response!=null){ // response.close(); // } // String id=sResponse.substring(sResponse.indexOf("'.")+3, sResponse.indexOf("';")); // System.out.println(id); // System.out.println(); // getHtml(id,f.listFiles()[i]); }else{ sw(f.listFiles()[i]); } } } public static void getHtml(String id,File file) { try { String ew[]=file.getName().split("\\."); String we=file.getParent().replace("C:\\Users\\Thinkpad\\Desktop\\com\\", "E:\\myProjects\\jspgou\\src\\com\\"); File f = new File(we); if(!f.exists()){ f.mkdirs(); } f = new File(we+"/"+ew[0]+".java"); f.createNewFile(); String we1=HttpClientUtil.get("http://javare.cn/"+id+"/de/"+ew[0]+".java"); System.out.println(we1); byte buf[]=we1.getBytes(); FileOutputStream out=new FileOutputStream(f); out.write(buf,0,buf.length); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
在线批量反编译工具
本文由 admin 创作,采用 知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:2022-08-09 21:12:07