我试图构建以下uri
http://localhost:8080/TestService.svc/RunTest我是这样做的
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost:8080/TestService.svc";
uriBuilder.Path = String.Format("/{0}", "RunTest");
string address = uriBuilder.ToString()
//In debugger the address looks like http://[http://localhost:8080/TestService.svc]/RunTest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);上面的内容会产生异常。
Invalid URI: The hostname could not be parsed.我将感谢你在解决这个问题上的帮助。
发布于 2014-11-04 13:47:22
在使用Uri构建器时,您需要将主机、端口和路径作为自己的行。另外,TestService.svc也是路径的一部分,而不是主机,如果不使用端口,则可以将它们分开。
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost";
uriBuilder.Port = 8080;
uriBuilder.Path = String.Format("/{0}/{1}", "TestService.svc", "RunTest");
var address = uriBuilder.ToString();发布于 2014-11-04 09:24:12
当我运行您的代码时,正如您所指出的,我也看到方括号作为address变量的值,但是我没有在生成的Uri中看到PerfTestService,也不知道为什么会这样?!我看到:
http://[localhost:8080/TestService.svc]/RunTest由于您已经知道主机和路径,我建议您将其构造为字符串。
var uriBuilder = new UriBuilder("http://localhost:8080/TestService.svc/RunTest");
string address = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);https://stackoverflow.com/questions/26731176
复制相似问题