/** * Creates absolute request URI with full path from passed in context, honoring proxy if in play. */ @Nonnull private URI getRequestURI(final HttpContext context) { final HttpClientContext clientContext = HttpClientContext.adapt(context); final HttpRequest httpRequest = clientContext.getRequest(); try { URI uri; if (httpRequest instanceof HttpUriRequest) { uri = ((HttpUriRequest) httpRequest).getURI(); } else { uri = URI.create(httpRequest.getRequestLine().getUri()); } final RouteInfo routeInfo = clientContext.getHttpRoute(); if (routeInfo != null) { if (routeInfo.getHopCount() == 1 && uri.isAbsolute()) { return uri; } HttpHost target = routeInfo.getHopTarget(0); return URIUtils.resolve(URI.create(target.toURI()), uri); } else { return uri; } } catch (Exception e) { log.warn("Could not create absolute request URI", e); return URI.create(clientContext.getTargetHost().toURI()); } }
/** * GET方式请求url * * @param url 请求地址 * @param params 请求参数 * @return url响应结果,java.lang.String类型。 * @throws java.net.URISyntaxException 输入的url不合法 * @throws java.io.IOException */ public String get(String url, List<NameValuePair> params) throws URISyntaxException, IOException { // 1. 验证输入url的有效性:url没有有效的host或url为相对路径,则url无效。 URI uri = (new URIBuilder(url)).build(); HttpHost httpHost = URIUtils.extractHost(uri); if (httpHost == null) { throw new IllegalArgumentException("缺少有效的HOST"); } String respText; // 2. 创建HttpClient对象 CloseableHttpClient client = getHttpClient(); // 3. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。 HttpGet httpGet = new HttpGet(uri); if (logger.isDebugEnabled()) { logger.debug("executing request: ", httpGet.getRequestLine()); } // 4. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数; // 对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。 if (params != null && params.size() > 0) { httpGet.setURI( new URI( httpGet.getURI().toString() + "?" + EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8)))); } // 5. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。 respText = execute(client, httpHost, httpGet); return respText; }
ValidationResult validateUri(URL url, ValidationRequest request) throws IOException { String parser = request.getValue("parser", null); HttpRequestBase method; List<NameValuePair> qParams = new ArrayList<NameValuePair>(); qParams.add(new BasicNameValuePair("out", "json")); if (parser != null) { qParams.add(new BasicNameValuePair("parser", parser)); } qParams.add(new BasicNameValuePair("doc", url.toString())); try { URI uri = new URI(baseUrl); URI uri2 = URIUtils.createURI( uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), URLEncodedUtils.format(qParams, "UTF-8"), null); method = new HttpGet(uri2); return validate(method); } catch (URISyntaxException e) { throw new IllegalArgumentException("invalid uri. Check your baseUrl " + baseUrl, e); } }
public Object get( String url, AbstractAsyncResponseConsumer myConsumer, Map<String, String> headerMap) throws IOException { URI uri = URI.create(url); CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); Object obj = null; try { httpclient.start(); HttpGet httpget = new HttpGet(uri); HttpHost httphost = URIUtils.extractHost(uri); if (headerMap != null) { for (String key : headerMap.keySet()) { httpget.addHeader(key, headerMap.get(key)); } } BasicAsyncRequestProducer requset = new BasicAsyncRequestProducer(httphost, httpget); Future<Object> future = httpclient.execute(requset, myConsumer, null); obj = future.get(); } catch (InterruptedException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (ExecutionException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } finally { httpclient.close(); return obj; } }
private static URI uri(final String graph) throws URISyntaxException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("graph", graph)); URI uri = URIUtils.createURI(SCHEME, HOST, PORT, PATH, URLEncodedUtils.format(params, "UTF-8"), null); return uri; }
/** * @param path relative path for the resource including / prefix * @param qparams NameValuePair list of parameters * @return a correctly formatted and urlencoded string * @throws URISyntaxException */ public static URI getUri(String scheme, String path, List<NameValuePair> qparams) throws URISyntaxException { return URIUtils.createURI( scheme, Constants.Backend.API_HOST, -1, path, URLEncodedUtils.format(qparams, "UTF-8"), null); }
/** * Used to build an mp3 skull search query. An initial request might be needed to fetch the CSRF * token from the page source before searching * * @param query * @param csrf * @return * @throws URISyntaxException */ public static URI getMp3SkullSearchURI(String query, String csrf) throws URISyntaxException { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", query)); qparams.add(new BasicNameValuePair("fckh", csrf)); return URIUtils.createURI( Constants.Mp3skull.API_SCHEME, Constants.Mp3skull.API_HOST, -1, Constants.Mp3skull.API_SEARCH, URLEncodedUtils.format(qparams, "UTF-8"), null); }
public void parse(int eLevel, boolean show_closed, BoundingBoxE6 boundingBox) { // get a factory SAXParserFactory spf = SAXParserFactory.newInstance(); try { // get a new instance of parser SAXParser sp = spf.newSAXParser(); // parse the file and also register this class for call backs HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add( new BasicNameValuePair("l", "" + String.valueOf(boundingBox.getLonWestE6() / 1E6))); qparams.add( new BasicNameValuePair("b", "" + String.valueOf(boundingBox.getLatSouthE6() / 1E6))); qparams.add( new BasicNameValuePair("r", "" + String.valueOf(boundingBox.getLonEastE6() / 1E6))); qparams.add( new BasicNameValuePair("t", "" + String.valueOf(boundingBox.getLatNorthE6() / 1E6))); if (!show_closed) { qparams.add(new BasicNameValuePair("open", "1")); } URI uri; uri = URIUtils.createURI( "http", "openstreetbugs.schokokeks.org", -1, "/api/0.1/getGPX", URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet httpget = new HttpGet(uri); HttpResponse response = httpClient.execute(httpget, localContext); org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(OpenFixMapActivity.class); logger.info("Fetch " + httpget.getURI()); sp.parse(response.getEntity().getContent(), this); } catch (SAXException se) { se.printStackTrace(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Prepare Vimeo search URL * * @param query * @param page * @return * @throws URISyntaxException */ public static URI getVimeoSearchURI(String query, Integer page) throws URISyntaxException { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("query", query)); qparams.add(new BasicNameValuePair("page", String.valueOf(page))); qparams.add(new BasicNameValuePair("per_page", String.valueOf(Constants.Vimeo.PER_PAGE))); return URIUtils.createURI( Constants.Vimeo.API_SCHEME, Constants.Vimeo.API_HOST, -1, Constants.Vimeo.API_SEARCH, URLEncodedUtils.format(qparams, "UTF-8"), null); }
/** * Prepare soundcloud search URL * * @param query * @param offset * @return * @throws URISyntaxException */ public static URI getSoundCloudSearchURI(String query, Integer offset) throws URISyntaxException { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("client_id", Constants.Soundcloud.CLIENT_ID)); qparams.add(new BasicNameValuePair("offset", String.valueOf(offset))); qparams.add(new BasicNameValuePair("limit", String.valueOf(Constants.Soundcloud.PER_PAGE))); qparams.add(new BasicNameValuePair("q", query)); return URIUtils.createURI( Constants.Soundcloud.API_SCHEME, Constants.Soundcloud.API_HOST, -1, Constants.Soundcloud.API_SEARCH, URLEncodedUtils.format(qparams, "UTF-8"), null); }
// Get a token public JSONObject getToken(String transaction, String amount) throws JSONException { HttpClient http = new DefaultHttpClient(); URI uri; try { uri = URIUtils.createURI(URL_SCHEME, SERVER_LOCATION, -1, TOKEN_PATH, null, null); } catch (URISyntaxException e) { Log.e(TAG, "Malformed URL", e); return null; } HttpPost post = new HttpPost(uri); post.setHeader("Set-Cookie", "_id=" + mSessionId); List<NameValuePair> postData = new ArrayList<NameValuePair>(); postData.add(new BasicNameValuePair("transaction", transaction)); postData.add(new BasicNameValuePair("amount", amount)); postData.add(new BasicNameValuePair("sid", mSessionId)); Log.d(TAG, "Session ID: " + mSessionId); Log.d(TAG, "Transaction: " + transaction); try { post.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8)); HttpResponse response = http.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String responseStr = ""; String line = ""; while ((line = rd.readLine()) != null) { responseStr += line; } if (responseStr.equals("false")) { Log.w(TAG, "Bad token request"); return null; } Log.d(TAG, responseStr); try { JSONObject result = new JSONObject(responseStr); return result; } catch (JSONException e) { Log.e(TAG, "could not parse JSON response", e); return null; } } catch (UnsupportedEncodingException e) { Log.e(TAG, "Could not encode request parameters", e); return null; } catch (IOException e) { Log.e(TAG, "Error sending HTTP request", e); return null; } }
protected String getListRequestURL(ArrayList<NameValuePair> params) throws URISyntaxException, IOException { URI u = URIUtils.createURI( "http", "ep.knou.ac.kr", -1, "portal/epo/service/retrieveIntgAnncList.data", URLEncodedUtils.format(params, "UTF-8"), null); HelpF.Log.d(this, "u.toURL():" + u.toURL()); // HttpPost post = new HttpPost(u); HttpGet get = new HttpGet(u); // HttpPost post = new // HttpPost("http://api.twitter.com/1/direct_messages/new.xml"); // 한글처리 // post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = mHttpClient.execute(get); int code = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); StringBuilder sb = new StringBuilder(); java.io.InputStreamReader isr = null; BufferedReader br = null; try { isr = new java.io.InputStreamReader(is, "UTF-8"); br = new BufferedReader(isr); String tmp = null; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (Exception ex) { } finally { try { br.close(); } catch (IOException ex) { } try { isr.close(); } catch (IOException ex) { } } String content = sb.toString(); HelpF.Log.d("HAN", "content1:" + content); return content; }
protected void rewriteRequestURI(final RequestWrapper request, final HttpRoute route) throws ProtocolException { try { URI uri = request.getURI(); if (route.getProxyHost() != null && !route.isTunnelled()) { // Make sure the request URI is absolute if (!uri.isAbsolute()) { HttpHost target = route.getTargetHost(); uri = URIUtils.rewriteURI(uri, target); request.setURI(uri); } } else { // Make sure the request URI is relative if (uri.isAbsolute()) { uri = URIUtils.rewriteURI(uri, null); request.setURI(uri); } } } catch (URISyntaxException ex) { throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex); } }
/** * Prepare a valid Youtube API request URI * * @param query * @param pageToken * @return * @throws URISyntaxException */ public static URI getYoutubeSearchURI(String query, String pageToken) throws URISyntaxException { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", query)); // if there is a specific page to load if (pageToken != null && pageToken.length() > 0) { qparams.add(new BasicNameValuePair("pageToken", pageToken)); } qparams.add(new BasicNameValuePair("part", "snippet,id")); qparams.add(new BasicNameValuePair("maxResults", String.valueOf(Constants.Youtube.PER_PAGE))); qparams.add(new BasicNameValuePair("safeSearch", Constants.Youtube.SAFE_SEARCH)); qparams.add(new BasicNameValuePair("type", Constants.Youtube.TYPE)); qparams.add(new BasicNameValuePair("key", Constants.Youtube.API_TOKEN)); return URIUtils.createURI( Constants.Youtube.API_SCHEME, Constants.Youtube.API_HOST, -1, Constants.Youtube.API_SEARCH, URLEncodedUtils.format(qparams, "UTF-8"), null); }
// Send a request to the server to get a session id private static String login(String username, String password) { HttpClient http = new DefaultHttpClient(); URI uri; try { uri = URIUtils.createURI(URL_SCHEME, SERVER_LOCATION, -1, LOGIN_PATH, null, null); } catch (URISyntaxException e) { Log.e(TAG, "Malformed URL", e); return null; } HttpPost post = new HttpPost(uri); List<NameValuePair> postData = new ArrayList<NameValuePair>(); postData.add(new BasicNameValuePair("email", username)); postData.add(new BasicNameValuePair("password", password)); try { post.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8)); HttpResponse response = http.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String responseStr = ""; String line = ""; while ((line = rd.readLine()) != null) { responseStr += line; } if (responseStr.equals("false")) { Log.w(TAG, "Problem logging in"); return null; } return responseStr; } catch (UnsupportedEncodingException e) { Log.e(TAG, "Could not encode request parameters", e); return null; } catch (IOException e) { Log.e(TAG, "Error sending HTTP request", e); return null; } }
// Ask the server if the token is OK public static boolean authorize(String token, String amount) { HttpClient http = new DefaultHttpClient(); URI uri; try { uri = URIUtils.createURI(URL_SCHEME, SERVER_LOCATION, -1, AUTH_PATH, null, null); } catch (URISyntaxException e) { Log.e(TAG, "Malformed URL", e); return false; } HttpPost post = new HttpPost(uri); List<NameValuePair> postData = new ArrayList<NameValuePair>(); postData.add(new BasicNameValuePair("amount", amount)); postData.add(new BasicNameValuePair("token", token)); try { post.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8)); HttpResponse response = http.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String responseStr = ""; String line = ""; while ((line = rd.readLine()) != null) { responseStr += line; } if (!responseStr.equals("true")) { Log.i(TAG, "Bad token"); return false; } Log.d(TAG, responseStr); return true; } catch (UnsupportedEncodingException e) { Log.e(TAG, "Could not encode request parameters", e); return false; } catch (IOException e) { Log.e(TAG, "Error sending HTTP request", e); return false; } }
/* (non-Javadoc) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { try { if (m_client == null) { connect(); } int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":"); HttpGet request = new HttpGet( URIUtils.createURI( m_url.getProtocol(), m_url.getHost(), port, m_url.getPath(), m_url.getQuery(), null)); if (userInfo != null) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo[0], userInfo[1]); request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false)); } HttpResponse response = m_client.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }
/** * Get the base Mp3Skull url to determine CSRF token for the session * * @return * @throws URISyntaxException */ public static URI getMp3SkullBaseURI() throws URISyntaxException { return URIUtils.createURI( Constants.Mp3skull.API_SCHEME, Constants.Mp3skull.API_HOST, -1, null, null, null); }
protected String getInfoRequestURL(ArrayList<NameValuePair> params) { // 학과 선택 URI u = null; try { u = URIUtils.createURI( "http", "ep.knou.ac.kr", -1, "/portal/epo/service/retrieveIntgAnncDtl.do", URLEncodedUtils.format(params, "UTF-8"), null); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { HelpF.Log.d("HAN", "u.toURL():" + u.toURL()); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } /* HttpPost post = new HttpPost(u); // 한글처리 try { post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } */ HttpGet get = new HttpGet(u); HttpResponse response = null; try { response = mHttpClient.execute(get); } catch (ClientProtocolException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int code = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); InputStream is = null; try { is = entity.getContent(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuilder sb = new StringBuilder(); java.io.InputStreamReader isr = null; BufferedReader br = null; try { isr = new java.io.InputStreamReader(is, "UTF-8"); br = new BufferedReader(isr); String tmp = null; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (Exception ex) { } finally { try { br.close(); } catch (IOException ex) { } try { isr.close(); } catch (IOException ex) { } } String content = sb.toString(); // Log.d("HAN","content1:"+content); content = content.substring(content.indexOf("<!-- view -->")); // <!-- 입력 내용 --> content = content.substring(0, content.lastIndexOf("<!-- // view -->")); HelpF.Log.d("HAN", "content1:" + content); return content; }
@SuppressWarnings("deprecation") @Override public void filterHttpRequest(SubmitContext context, HttpRequestInterface<?> request) { HttpRequestBase httpMethod = (HttpRequestBase) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD); String path = PropertyExpander.expandProperties(context, request.getPath()); StringBuffer query = new StringBuffer(); String encoding = System.getProperty("soapui.request.encoding", StringUtils.unquote(request.getEncoding())); StringToStringMap responseProperties = (StringToStringMap) context.getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES); MimeMultipart formMp = "multipart/form-data".equals(request.getMediaType()) && httpMethod instanceof HttpEntityEnclosingRequestBase ? new MimeMultipart() : null; RestParamsPropertyHolder params = request.getParams(); for (int c = 0; c < params.getPropertyCount(); c++) { RestParamProperty param = params.getPropertyAt(c); String value = PropertyExpander.expandProperties(context, param.getValue()); responseProperties.put(param.getName(), value); List<String> valueParts = sendEmptyParameters(request) || (!StringUtils.hasContent(value) && param.getRequired()) ? RestUtils.splitMultipleParametersEmptyIncluded( value, request.getMultiValueDelimiter()) : RestUtils.splitMultipleParameters(value, request.getMultiValueDelimiter()); // skip HEADER and TEMPLATE parameter encoding (TEMPLATE is encoded by // the URI handling further down) if (value != null && param.getStyle() != ParameterStyle.HEADER && param.getStyle() != ParameterStyle.TEMPLATE && !param.isDisableUrlEncoding()) { try { if (StringUtils.hasContent(encoding)) { value = URLEncoder.encode(value, encoding); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i), encoding)); } else { value = URLEncoder.encode(value); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i))); } } catch (UnsupportedEncodingException e1) { SoapUI.logError(e1); value = URLEncoder.encode(value); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i))); } // URLEncoder replaces space with "+", but we want "%20". value = value.replaceAll("\\+", "%20"); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, valueParts.get(i).replaceAll("\\+", "%20")); } if (param.getStyle() == ParameterStyle.QUERY && !sendEmptyParameters(request)) { if (!StringUtils.hasContent(value) && !param.getRequired()) continue; } switch (param.getStyle()) { case HEADER: for (String valuePart : valueParts) httpMethod.addHeader(param.getName(), valuePart); break; case QUERY: if (formMp == null || !request.isPostQueryString()) { for (String valuePart : valueParts) { if (query.length() > 0) query.append('&'); query.append(URLEncoder.encode(param.getName())); query.append('='); if (StringUtils.hasContent(valuePart)) query.append(valuePart); } } else { try { addFormMultipart( request, formMp, param.getName(), responseProperties.get(param.getName())); } catch (MessagingException e) { SoapUI.logError(e); } } break; case TEMPLATE: try { value = getEncodedValue( value, encoding, param.isDisableUrlEncoding(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); path = path.replaceAll("\\{" + param.getName() + "\\}", value == null ? "" : value); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } break; case MATRIX: try { value = getEncodedValue( value, encoding, param.isDisableUrlEncoding(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } if (param.getType().equals(XmlBoolean.type.getName())) { if (value.toUpperCase().equals("TRUE") || value.equals("1")) { path += ";" + param.getName(); } } else { path += ";" + param.getName(); if (StringUtils.hasContent(value)) { path += "=" + value; } } break; case PLAIN: break; } } if (request.getSettings().getBoolean(HttpSettings.FORWARD_SLASHES)) path = PathUtils.fixForwardSlashesInPath(path); if (PathUtils.isHttpPath(path)) { try { // URI(String) automatically URLencodes the input, so we need to // decode it first... URI uri = new URI(path, request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); context.setProperty(BaseHttpRequestTransport.REQUEST_URI, uri); java.net.URI oldUri = httpMethod.getURI(); httpMethod.setURI( HttpUtils.createUri( oldUri.getScheme(), oldUri.getRawUserInfo(), oldUri.getHost(), oldUri.getPort(), oldUri.getRawPath(), uri.getEscapedQuery(), oldUri.getRawFragment())); } catch (Exception e) { SoapUI.logError(e); } } else if (StringUtils.hasContent(path)) { try { java.net.URI oldUri = httpMethod.getURI(); String pathToSet = StringUtils.hasContent(oldUri.getRawPath()) && !"/".equals(oldUri.getRawPath()) ? oldUri.getRawPath() + path : path; java.net.URI newUri = URIUtils.createURI( oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), pathToSet, oldUri.getQuery(), oldUri.getFragment()); httpMethod.setURI(newUri); context.setProperty( BaseHttpRequestTransport.REQUEST_URI, new URI( newUri.toString(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS))); } catch (Exception e) { SoapUI.logError(e); } } if (query.length() > 0 && !request.isPostQueryString()) { try { java.net.URI oldUri = httpMethod.getURI(); httpMethod.setURI( URIUtils.createURI( oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), oldUri.getRawPath(), query.toString(), oldUri.getFragment())); } catch (Exception e) { SoapUI.logError(e); } } if (request instanceof RestRequest) { String acceptEncoding = ((RestRequest) request).getAccept(); if (StringUtils.hasContent(acceptEncoding)) { httpMethod.setHeader("Accept", acceptEncoding); } } if (formMp != null) { // create request message try { if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) { String requestContent = PropertyExpander.expandProperties( context, request.getRequestContent(), request.isEntitizeProperties()); if (StringUtils.hasContent(requestContent)) { initRootPart(request, requestContent, formMp); } } for (Attachment attachment : request.getAttachments()) { MimeBodyPart part = new PreencodedMimeBodyPart("binary"); if (attachment instanceof FileAttachment<?>) { String name = attachment.getName(); if (StringUtils.hasContent(attachment.getContentID()) && !name.equals(attachment.getContentID())) name = attachment.getContentID(); part.setDisposition( "form-data; name=\"" + name + "\"; filename=\"" + attachment.getName() + "\""); } else part.setDisposition("form-data; name=\"" + attachment.getName() + "\""); part.setDataHandler(new DataHandler(new AttachmentDataSource(attachment))); formMp.addBodyPart(part); } MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION); message.setContent(formMp); message.saveChanges(); RestRequestMimeMessageRequestEntity mimeMessageRequestEntity = new RestRequestMimeMessageRequestEntity(message, request); ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity); httpMethod.setHeader("Content-Type", mimeMessageRequestEntity.getContentType().getValue()); httpMethod.setHeader("MIME-Version", "1.0"); } catch (Throwable e) { SoapUI.logError(e); } } else if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) { if (StringUtils.hasContent(request.getMediaType())) httpMethod.setHeader( "Content-Type", getContentTypeHeader(request.getMediaType(), encoding)); if (request.isPostQueryString()) { try { ((HttpEntityEnclosingRequest) httpMethod).setEntity(new StringEntity(query.toString())); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } } else { String requestContent = PropertyExpander.expandProperties( context, request.getRequestContent(), request.isEntitizeProperties()); List<Attachment> attachments = new ArrayList<Attachment>(); for (Attachment attachment : request.getAttachments()) { if (attachment.getContentType().equals(request.getMediaType())) { attachments.add(attachment); } } if (StringUtils.hasContent(requestContent) && attachments.isEmpty()) { try { byte[] content = encoding == null ? requestContent.getBytes() : requestContent.getBytes(encoding); ((HttpEntityEnclosingRequest) httpMethod).setEntity(new ByteArrayEntity(content)); } catch (UnsupportedEncodingException e) { ((HttpEntityEnclosingRequest) httpMethod) .setEntity(new ByteArrayEntity(requestContent.getBytes())); } } else if (attachments.size() > 0) { try { MimeMultipart mp = null; if (StringUtils.hasContent(requestContent)) { mp = new MimeMultipart(); initRootPart(request, requestContent, mp); } else if (attachments.size() == 1) { ((HttpEntityEnclosingRequest) httpMethod) .setEntity(new InputStreamEntity(attachments.get(0).getInputStream(), -1)); httpMethod.setHeader( "Content-Type", getContentTypeHeader(request.getMediaType(), encoding)); } if (((HttpEntityEnclosingRequest) httpMethod).getEntity() == null) { if (mp == null) mp = new MimeMultipart(); // init mimeparts AttachmentUtils.addMimeParts(request, attachments, mp, new StringToStringMap()); // create request message MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION); message.setContent(mp); message.saveChanges(); RestRequestMimeMessageRequestEntity mimeMessageRequestEntity = new RestRequestMimeMessageRequestEntity(message, request); ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity); httpMethod.setHeader( "Content-Type", getContentTypeHeader( mimeMessageRequestEntity.getContentType().getValue(), encoding)); httpMethod.setHeader("MIME-Version", "1.0"); } } catch (Exception e) { SoapUI.logError(e); } } } } }
public void execute() throws Exception { HttpRequestBaseHC4 request; if (isPost()) { request = new HttpPostHC4(uri); if (onHttpRequestListener != null) onHttpRequestListener.onRequest(this); if (!requestParams.isEmpty()) { try { ((HttpPostHC4) request).setEntity(new UrlEncodedFormEntity(requestParams, "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } else { if (onHttpRequestListener != null) onHttpRequestListener.onRequest(this); if (!requestParams.isEmpty()) { try { URI oldUri = new URI(uri); String query = ""; if (oldUri.getQuery() != null && !oldUri.getQuery().equals("")) query += oldUri.getQuery() + "&"; query += URLEncodedUtils.format(requestParams, "UTF-8"); URI newUri = URIUtils.createURI( oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), oldUri.getPath(), query, null); uri = newUri.toString(); } catch (URISyntaxException e) { e.printStackTrace(); } } request = new HttpGetHC4(uri); } RequestConfig rc = RequestConfig.custom() .setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectionRequestTimeout) .build(); request.setConfig(rc); try { CloseableHttpResponse response = CpHttpClient.getHttpClient().execute(request, context); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); response.getEntity().writeTo(baos); // 将数据转换为字符串保存 String respCharset = EntityUtils.getContentCharSet(response.getEntity()); if (respCharset != null) result = new String(baos.toByteArray(), respCharset); else result = new String(baos.toByteArray(), "UTF-8"); if (onHttpRequestListener != null) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) onHttpRequestListener.onSucceed(statusCode, this); else onHttpRequestListener.onFailed(statusCode, this); } } finally { response.close(); } } catch (IOException e) { e.printStackTrace(); CpHttpClient.shutDown(); } }
public static URI API_formaturi(String path) throws Exception { URI uri = URIUtils.createURI("https", "cloudapi.acquia.com/v1", -1, path, null, null); return uri; }
private void getFacebookResults(Keyword k) { log.debug("name: " + k.getValue()); List<NameValuePair> params = new ArrayList<NameValuePair>(); String keywordValue = k.getValue().replace(" ", "+"); params.add(new BasicNameValuePair("q", keywordValue)); params.add(new BasicNameValuePair("limit", "200")); String query = URLEncodedUtils.format(params, "utf-8"); SearchSession searchSession = new SearchSession(); searchSession.setStartDate(Calendar.getInstance().getTime()); searchSession.setSearchText(query); searchSessionRepository.create(searchSession); URI url = null; try { url = URIUtils.createURI("https", "graph.facebook.com", 0, "search", query, null); log.debug(url.toString()); String respRow = getStringFromUrl(url, 3); searchSession.setRawData(respRow); Gson gson = new Gson(); FacebookResponse respList = gson.fromJson(respRow, FacebookResponse.class); for (FBData d : respList.data) { boolean validText = false; String body = new String(d.message.getBytes("UTF-8"), "UTF-8"); if (body.length() > 2) { boolean resp = dictionaryService.valideText(body, 50); if (resp) { validText = true; } else { log.debug("nem magyar szoveg: " + body); } } else { log.debug("a body nem eleg hosszu "); } if (validText) { DateFormat formatter; Date originalDate; formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); originalDate = formatter.parse(d.created_time); String title = (d.name != null && d.name.length() > 100) ? d.name.substring(0, 100) : d.name; dataService.createData( d.id, body, url.toString(), title, "facebook", searchSession, originalDate, k); } } searchSession.setEndDate(Calendar.getInstance().getTime()); searchSessionRepository.update(searchSession); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } // get the location header to find out where to redirect to Header locationHeader = response.getFirstHeader("location"); if (locationHeader == null) { // got a redirect response, but no location header throw new ProtocolException( "Received redirect response " + response.getStatusLine() + " but no location header"); } // HERE IS THE MODIFIED LINE OF CODE String location = locationHeader.getValue().replaceAll(" ", "%20"); URI uri; try { uri = new URI(location); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } HttpParams params = response.getParams(); // rfc2616 demands the location value be a complete URI // Location = "Location" ":" absoluteURI if (!uri.isAbsolute()) { if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) { throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"); } // Adjust location URI HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { throw new IllegalStateException("Target host not available " + "in the HTTP context"); } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); try { URI requestURI = new URI(request.getRequestLine().getUri()); URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); uri = URIUtils.resolve(absoluteRequestURI, uri); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) { RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); context.setAttribute(REDIRECT_LOCATIONS, redirectLocations); } URI redirectURI; if (uri.getFragment() != null) { try { HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); redirectURI = URIUtils.rewriteURI(uri, target, true); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } else { redirectURI = uri; } if (redirectLocations.contains(redirectURI)) { throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'"); } else { redirectLocations.add(redirectURI); } } return uri; }
private void getTwitterResults(Keyword k) { log.debug("name: " + k.getValue()); List<NameValuePair> params = new ArrayList<NameValuePair>(); String keywordValue = k.getValue().replace(" ", "+"); params.add(new BasicNameValuePair("q", keywordValue)); String query = URLEncodedUtils.format(params, "utf-8"); URI url = null; try { SearchSession searchSession = new SearchSession(); searchSession.setStartDate(Calendar.getInstance().getTime()); url = URIUtils.createURI("http", "search.twitter.com", 0, "search.json", query, null); searchSession.setSearchText(url.toString()); log.debug(url.toString()); Gson gson = new Gson(); String respRow = getStringFromUrl(url, 3); TwitterResponse respList = gson.fromJson(respRow, TwitterResponse.class); // System.out.println(respList); searchSession.setRawData(respRow); searchSessionRepository.create(searchSession); for (TwitterResult d : respList.results) { // ellenorzi sourceid alapjan, hogy szerepel-e az // adatbaziban String body = URLDecoder.decode(d.text, "UTF-8"); boolean validText = false; if (body.length() > 2) { boolean resp = dictionaryService.valideText(body, 50); if (resp) { validText = true; } else { log.debug("nem magyar szoveg: " + body); } } else { log.debug("a body nem eleg hosszu "); } log.debug(d.id); if (validText) { DateFormat formatter; Date originalDate; formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); originalDate = formatter.parse(d.created_at); dataService.createData( d.id, body, url.toString(), d.to_user, "twitter", searchSession, originalDate, k); } } searchSession.setEndDate(Calendar.getInstance().getTime()); searchSessionRepository.update(searchSession); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }