zoukankan      html  css  js  c++  java
  • 测试攻城师的一点小开心

        String strUrl="http://192.168.16.xxx:8088/funadx-web/xxx/xxx/xxx/xxx";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(strUrl);

    运行后,出现异常:java.net.URISyntaxException
     
    网上找资料,原来是地址中涉及了特殊字符“-”,所以只能先把String转成URL,再能过URL生成URI的方法来解决问题 
    修改代码如下:
        URL url = new URL(strUrl);
        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(uri);
     
    调试发现,post的时候端口不见了。。。
    真真有点郁闷呀,还好我当时想这个肯定是可以设置端口的,但是怎么样设置端口呢?百度了一下,没找到解决方案(可能是我搜索时问题描述的不对)
    还好此时想起程序猿男票说他看开源代码的好处。。。额。要不我看下源码。
    哈哈,好开心,原来URI还有个构造函数,可以设置端口。bingo,问题解决。
    最终代码如下:
        URL postUrl = new URL(strUrl);
        URI uri = new URI(url.getProtocol(), null, url.getHost(),url.getPort(),url.getPath(), url.getQuery(), null );
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(uri);
     
    这次虽然解决的不是一个很难的问题,但对于我来说也算一个小突破了,还是小开心的,mark一下。
  • 相关阅读:
    ReactiveCocoa 谈谈RACMulticastConnection
    ReactiveCocoa 谈谈concat
    Swift 一些环境配置
    hpple 简单使用
    Swift 学习手记1,pod 的 类库使用
    [转]SQL语句:Group By总结
    Jquery VailDate初探
    C#RSA加密解密详解
    电子印章制作管理系统 -升级版本
    tensorflow 实现的第一个目标检测模型
  • 原文地址:https://www.cnblogs.com/weiweiyao/p/4384105.html
Copyright © 2011-2022 走看看