java.net.URISyntaxException: Illegal character in scheme name at index 0

在做微信公众号服务,网页获取用户授权的时候报错,如图:
在这里插入图片描述
经过搜索,发现是没有对url里的代码进行编码,因为url本身带有?&等字符,所以需要用URLEncoder进行编译。

		String url=" https://api.weixin.qq.com/sns/oauth2/access_token" +
					"?" +Wechat_Appid+Wechat_Secret+
					"&code=" +code+
					"&grant_type=authorization_code";
		url= URLEncoder.encode(url,"UTF-8");
		JSONObject jsonObject = HttpUntil.sendPostNoreturn(url);

还有其他方式修改:

import java.net.*;

public class Test {
  public static void main(String[] args) {
    String myURL = "http://finance.yahoo.com/q/h?s=^IXIC";
    try {
      URL url = new URL(myURL);
      String nullFragment = null;
      URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), nullFragment);
      System.out.println("URI " + uri.toString() + " is OK");
    } catch (MalformedURLException e) {
      System.out.println("URL " + myURL + " is a malformed URL");
    } catch (URISyntaxException e) {
      System.out.println("URI " + myURL + " is a malformed URL");
    }
  }
}
import java.net.*;
import java.io.*;

public class EncodeParameter { 

    public static void main( String [] args ) throws URISyntaxException ,
                                         UnsupportedEncodingException   { 

        String myQuery = "^IXIC";

        URI uri = new URI( String.format( 
                           "http://finance.yahoo.com/q/h?s=%s", 
                           URLEncoder.encode( myQuery , "UTF8" ) ) );

        System.out.println( uri );

    }
}

详细请点击

你可能感兴趣的:(Java)