本文整理匯總了Java中java.net.URL.getUserInfo方法的典型用法代碼示例。如果您正苦於以下問題:Java URL.getUserInfo方法的具體用法?Java URL.getUserInfo怎麽用?Java URL.getUserInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.URL
的用法示例。
在下文中一共展示了URL.getUserInfo方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encodeUrlToHttpFormat
import java.net.URL; //導入方法依賴的package包/類
public static String encodeUrlToHttpFormat(String urlString)
{
String encodedUrl=null;
try
{
URL url=new URL(urlString);
URI uri= new URI(url.getProtocol(),url.getUserInfo(),url.getHost(),url.getPort(),url.getPath(),
url.getQuery(),url.getRef());
//System.out.println(uri.toURL().toString());
encodedUrl=uri.toURL().toString();
}
catch (Exception exc)
{
return null;
}
return encodedUrl;
}
示例2: getUrlWithQueryString
import java.net.URL; //導入方法依賴的package包/類
/**
* Will encode url, if not disabled, and adds params on the end of it
*
* @param url String with URL, should be valid URL without params
* @param params RequestParams to be appended on the end of URL
* @param shouldEncodeUrl whether url should be encoded (replaces spaces with %20)
* @return encoded url if requested with params appended if any available
*/
public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) {
if (url == null)
return null;
if (shouldEncodeUrl) {
try {
String decodedURL = URLDecoder.decode(url, "UTF-8");
URL _url = new URL(decodedURL);
URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(), _url.getPath(), _url.getQuery(), _url.getRef());
url = _uri.toASCIIString();
} catch (Exception ex) {
// Should not really happen, added just for sake of validity
log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
}
}
if (params != null) {
// Construct the query string and trim it, in case it
// includes any excessive white spaces.
String paramString = params.getParamString().trim();
// Only add the query string if it isn't empty and it
// isn't equal to '?'.
if (!paramString.equals("") && !paramString.equals("?")) {
url += url.contains("?") ? "&" : "?";
url += paramString;
}
}
return url;
}
示例3: initializeProxy
import java.net.URL; //導入方法依賴的package包/類
@PostConstruct
private void initializeProxy() {
if (!isBlank(proxyUrlEnv)) {
try {
URL proxyUrl = new URL(proxyUrlEnv);
String authString = proxyUrl.getUserInfo();
user = authString.split(":")[0];
password = authString.split(":")[1];
host = proxyUrl.getHost();
port = proxyUrl.getPort();
auth = new ProxyAuthenticator(user, password);
setProxy();
log.info("Set up global HTTP/HTTPS proxy to host:port {}:{} ", this.host, this.port);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} else {
log.warn("Environemnt variable QUOTAGUARDSTATIC_URL is not set, not configuring proxy!");
}
}
示例4: FtpURLConnection
import java.net.URL; //導入方法依賴的package包/類
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(url);
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
示例5: readURL
import java.net.URL; //導入方法依賴的package包/類
public static String readURL(String rawURL) throws IOException {
try
{
URL url = new URL(rawURL);
URLConnection urlConnection = url.openConnection();
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(url.getUserInfo().getBytes()));
urlConnection.setRequestProperty("Authorization", basicAuth);
}
InputStream inputStream = urlConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String inputLine = in.readLine();
in.close();
inputStream.close();
return inputLine;
} catch (IOException e)
{
throw e;
}
}
示例6: MiniFtp
import java.net.URL; //導入方法依賴的package包/類
/**
* Initial MiniFtp by url string.
*
* @param urlStr
* @throws Exception
*/
public MiniFtp(String urlStr) throws Exception {
URL url = new URL(urlStr);
/** Parse user info. */
String userInfo = url.getUserInfo();
if (userInfo != null && !"".equals(userInfo)) {
String[] userInfoArray = userInfo.split(":");
user = userInfoArray[0];
password = userInfoArray[1];
}
/** Parse host and port. */
commandPort = url.getPort();
host = url.getHost();
/** Parse file and type. */
file = url.getPath();
if (file.contains(";")) {
type = file.substring(file.lastIndexOf(";") + 1).replace("type=", "");
file = file.substring(0, file.lastIndexOf(";"));
}
}
示例7: getProxyExecutor
import java.net.URL; //導入方法依賴的package包/類
public static HttpCommandExecutor getProxyExecutor(URL url, Properties prop) {
prop = decrypt(prop);
String proxyHost = prop.getProperty("proxyHost");
int proxyPort = Integer.valueOf(prop.getProperty("proxyPort"));
String proxyUserDomain = prop.getProperty("proxyUserDomain");
String proxyUser = prop.getProperty("proxyUser");
String proxyPassword = prop.getProperty("proxyPassword");
HttpClientBuilder builder = HttpClientBuilder.create();
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
new NTCredentials(proxyUser, proxyPassword, getWorkstation(), proxyUserDomain));
if (url.getUserInfo() != null && !url.getUserInfo().isEmpty()) {
credsProvider.setCredentials(new AuthScope(url.getHost(), (url.getPort() > 0 ? url.getPort() : url.getDefaultPort())),
new UsernamePasswordCredentials(proxyUser, proxyPassword));
}
builder.setProxy(proxy);
builder.setDefaultCredentialsProvider(credsProvider);
HttpClient.Factory factory = new SimpleHttpClientFactory(builder);
return new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);
}
示例8: getAuthenticationFromURL
import java.net.URL; //導入方法依賴的package包/類
private PasswordAuthentication getAuthenticationFromURL() {
URL u = this.getRequestingURL();
if (u != null) {
String auth = u.getUserInfo();
if (auth != null) {
int i = auth.indexOf(':');
String user = (i == -1) ? auth : auth.substring(0, i);
String pwd = (i == -1) ? "" : auth.substring(i + 1);
return new PasswordAuthentication(user, pwd.toCharArray());
}
}
return null;
}
示例9: getAuthentication
import java.net.URL; //導入方法依賴的package包/類
@Override
public PasswordAuthentication
getAuthentication(
String realm,
URL tracker )
{
if ( user_name == null || password == null ){
String user_info = tracker.getUserInfo();
if ( user_info == null ){
return( null );
}
String user_bit = user_info;
String pw_bit = "";
int pos = user_info.indexOf(':');
if ( pos != -1 ){
user_bit = user_info.substring(0,pos);
pw_bit = user_info.substring(pos+1);
}
return( new PasswordAuthentication( user_bit, pw_bit.toCharArray()));
}
return( new PasswordAuthentication( user_name, password.toCharArray()));
}
示例10: calculateNonProxyUri
import java.net.URL; //導入方法依賴的package包/類
private static URI calculateNonProxyUri(HttpServletRequest request) throws MalformedURLException, URISyntaxException {
URL url = new URL(request.getRequestURL().toString());
String host = url.getHost();
String scheme = url.getProtocol();
int port = url.getPort();
String userInfo = url.getUserInfo();
return new URI(scheme, userInfo, host, port, urlPathHelper.getContextPath(request), null, null);
}
示例11: MultipartUtility
import java.net.URL; //導入方法依賴的package包/類
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
* @param requestURL
* @param charset
* @throws IOException
*/
public MultipartUtility(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "MaBite");
//Basic auth auto
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(url.getUserInfo().getBytes()));
httpConn.setRequestProperty("Authorization", basicAuth);
}
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
示例12: ProxyInformation
import java.net.URL; //導入方法依賴的package包/類
public ProxyInformation(String rawProxy, Optional<String> nonProxyHosts) {
try {
URL proxyUrl = new URL(rawProxy);
String userInfo = proxyUrl.getUserInfo();
if (userInfo != null && !userInfo.isEmpty()) {
String[] parts = userInfo.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException(String.format("Malformed proxy URL '%s'", rawProxy));
}
this.username = Optional.of(parts[0]);
this.password = Optional.of(parts[1]);
} else {
this.username = Optional.empty();
this.password = Optional.empty();
}
this.host = proxyUrl.getHost();
this.port = proxyUrl.getPort();
// Turn "foo, .other.com" into ["foo", "*.other.com"]
this.nonProxyHosts = nonProxyHosts.map(
s -> Arrays.stream(s.split(","))
.map(host -> host.trim().replaceAll("^\\.", "*."))
.collect(Collectors.toList())
).orElseGet(ArrayList::new);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(String.format("Malformed proxy URL '%s'", rawProxy), e);
}
}
示例13: test
import java.net.URL; //導入方法依賴的package包/類
@Test
public void test() throws JSONException, IOException {
String requestUrl = "http://elastic:[email protected]:9200/log_idx/_search?sort=dateTime:desc&size=1";
URL url = new URL(requestUrl);
URLConnection urlConnection = url.openConnection();
if( url.getUserInfo() != null && url.getUserInfo().split(":").length==2 ) {
LOGGER.info("Basic authentication usr/pwd > " + url.getUserInfo());
String name = url.getUserInfo().split(":")[0];
String password = url.getUserInfo().split(":")[1];
byte[] authEncBytes = Base64.getEncoder().encode( (name + ":" + password).getBytes() );
urlConnection.setRequestProperty("Authorization", "Basic " + new String(authEncBytes));
}
InputStream is = urlConnection.getInputStream();
String xmlResp = XML.toString(new JSONObject(IOUtils.toString(is)));
StringBuilder response = new StringBuilder().append("<root>").append(xmlResp).append("</root>");
LOGGER.info( response );
// HTTPGetConnector httpget = HTTPGetConnector();
// http://elastic:[email protected]:9200/log_idx/_search?sort=dateTime:desc&size=1
}
示例14: URLBuilder
import java.net.URL; //導入方法依賴的package包/類
/**
* Constructor.
*
* @param baseURL URL to parse and use as basis for creating other URLs
*
* @throws IllegalArgumentException thrown if the given base URL is not well formed
*/
public URLBuilder(String baseURL) {
try {
URL url = new URL(baseURL);
setScheme(url.getProtocol());
String userInfo = url.getUserInfo();
if (!DatatypeHelper.isEmpty(userInfo)) {
if (userInfo.contains(":")) {
String[] userInfoComps = userInfo.split(":");
setUsername(HTTPTransportUtils.urlDecode(userInfoComps[0]));
setPassword(HTTPTransportUtils.urlDecode(userInfoComps[1]));
} else {
setUsername(userInfo);
}
}
setHost(url.getHost());
setPort(url.getPort());
setPath(url.getPath());
queryParams = new ArrayList<Pair<String, String>>();
String queryString = url.getQuery();
if (!DatatypeHelper.isEmpty(queryString)) {
String[] queryComps = queryString.split("&");
String queryComp;
String[] paramComps;
String paramName;
String paramValue;
for (int i = 0; i < queryComps.length; i++) {
queryComp = queryComps[i];
if (!queryComp.contains("=")) {
paramName = HTTPTransportUtils.urlDecode(queryComp);
queryParams.add(new Pair<String, String>(paramName, null));
} else {
paramComps = queryComp.split("=");
paramName = HTTPTransportUtils.urlDecode(paramComps[0]);
paramValue = HTTPTransportUtils.urlDecode(paramComps[1]);
queryParams.add(new Pair<String, String>(paramName, paramValue));
}
}
}
setFragment(url.getRef());
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Given URL is not well formed", e);
}
}
示例15: equals
import java.net.URL; //導入方法依賴的package包/類
protected boolean equals(URL u1, URL u2) {
String userInfo1 = u1.getUserInfo();
String userInfo2 = u2.getUserInfo();
return super.equals(u1, u2) &&
(userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
}