要从字符串创建Uri,您可以执行以下操作:
Uri u = new Uri("example.com");
Run Code Online (Sandbox Code Playgroud)
但问题是如果字符串(如上所述)不包含协议,您将获得异常:" Invalid URI: The format of the URI could not be determined."
为避免异常,您应该确保字符串包含协议,如下所示:
Uri u = new Uri("http://example.com");
Run Code Online (Sandbox Code Playgroud)
但是如果你把url作为输入,如果它丢失了,你如何添加协议呢?
我的意思是除了一些IndexOf/Substring操作?
优雅而快速的东西?
假设我有这样的网址:
http://www.example.com?key=123&KEY=198
Run Code Online (Sandbox Code Playgroud)
那将是什么结果
request.querystring("key")
and
request.querystring("KEY")
Run Code Online (Sandbox Code Playgroud)
我有点困惑.
我有一个字符串表示包含空格的URL,并希望将其转换为URI对象.如果是简单的尝试
String myString = "http://myhost.com/media/File Name that has spaces inside.mp3";
URI myUri = new URI(myString);
Run Code Online (Sandbox Code Playgroud)
它给了我
java.net.URISyntaxException: Illegal character in path at index X
Run Code Online (Sandbox Code Playgroud)
其中index X是URL字符串中第一个空格的位置.
如何可以解析myString到一个URI对象?
我试图FileInputStream在用户从图片库中选择的图像上获取一个对象.这是由URI返回的androidandroid.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
内容://媒体/外部/图像/媒体/ 3
当我尝试从这个对象构造一个java URI对象时,我得到一个IllegalArgumentException,其中包含URI中的异常描述Expected file scheme:content:// media/external/images/media/3而android URI将该方案显示为内容
从未找到原始问题的解决方案.但是如果你想要图片库中图像的字节流,这段代码就可以做到.
content://media/external/images/media/3
Run Code Online (Sandbox Code Playgroud) 我有这个:
http://127.0.0.1:8000/found-locations/?state=--&km=km
Run Code Online (Sandbox Code Playgroud)
我要这个:
found-locations/?state=--&km=km
Run Code Online (Sandbox Code Playgroud)
我如何在JavaScript中执行此操作?
我试过window.location.href但它给了我整个网址
我试过window.location.pathname.substr(1)但它给了我found-locations/
我正在寻找伪蜘蛛网站的方法.关键是我实际上并不想要内容,而是一个简单的URI列表.我可以使用该选项与Wget合理地接近这个想法--spider,但是当通过a输出输出时grep,我似乎无法找到合适的魔法来使其工作:
wget --spider --force-html -r -l1 http://somesite.com | grep 'Saving to:'
Run Code Online (Sandbox Code Playgroud)
该grep过滤器似乎完全没有对影响wget输出.我有什么不对或者是否有其他工具我应该尝试更适合提供这种有限的结果集?
UPDATE
所以我发现离线时默认情况下会wget写入stderr.我在手册页中错过了(事实上,如果它在那里我仍然没有找到它).一旦我把回归到stdout,我就更接近我需要的东西了:
wget --spider --force-html -r -l1 http://somesite.com 2>&1 | grep 'Saving to:'
Run Code Online (Sandbox Code Playgroud)
如果有的话,我仍然会对其他/更好的手段感兴趣.
假设uri的方案是"文件".还假设路径以'.'开头.
示例路径是'./.bashrc'.fulluri看起来怎么样?'file://./.bashrc'对我来说很奇怪.
我需要一个具有日期时间值的动作参数?有没有标准的方法来做到这一点?我需要有类似的东西:
mysite/Controller/Action/21-9-2009 10:20
Run Code Online (Sandbox Code Playgroud)
但我只是成功地用以下东西来代替:
mysite/Controller/Action/200909211020
Run Code Online (Sandbox Code Playgroud)
并编写一个自定义函数来处理这种格式.
再次,寻找一个标准或认可的ASP.net MVC方式来做到这一点.
似乎Windows坚持\在文件路径中写反斜杠,而.NET的URI类用斜杠写它们/.有没有正确的方法,即使在最原始的系统中也可以接受?为什么.NET的URI显示与Windows其他部分相比的其他斜杠?
Uri下面用作参数的程序集限定字符串在XAML中工作,但在代码中使用时会显示错误.
我尝试了各种各样的UriKind同样的结果.我怎样才能解决这个问题?
[Test]
public void LargeImageSource_IsKnown()
{
var uri = new Uri(
"pack://application:,,,/" +
"MyAssembly.Core.Presentation.Wpf;component/" +
"Images/Delete.png", UriKind.RelativeOrAbsolute);
Assert.That(
_pickerActivityCollectionVm.DeleteActivityCommand.LargeImageSource,
Is.EqualTo(uri));
}
System.UriFormatException : Invalid URI: Invalid port specified.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind)
Run Code Online (Sandbox Code Playgroud)
基于Thomas的精湛答案以及我对可读性的评论,我在BaseTestFixture类中使用了以下内容.希望这有助于其他人.
protected virtual void OnFixtureSetUp() {
// logging, other one time setup stuff...
const string scheme = "pack";
if (!UriParser.IsKnownScheme(scheme)) {
Assert.That(PackUriHelper.UriSchemePack, Is.EqualTo(scheme));
}
}
Run Code Online (Sandbox Code Playgroud) uri ×10
url ×3
android ×2
.net ×1
asp.net-mvc ×1
backslash ×1
c# ×1
datetime ×1
file ×1
file-uri ×1
grep ×1
http ×1
httprequest ×1
java ×1
javascript ×1
path ×1
query-string ×1
resources ×1
url-scheme ×1
web-crawler ×1
wget ×1
wpf ×1
xaml ×1