当我向来自GAE的身份验证的URL发送POST请求时,我在urlConn.getResponseCode()函数上收到MalformedURLException。
此问题仅在部署服务器上出现,并且在本地服务器上运行代码时不会出现。
Invalid URL specified: https://user:passtoken@twilix.exotel.in/v1/Accounts/user/Sms/send
java.net.MalformedURLException: Invalid URL specified: https://user:passtoken@twilix.exotel.in/v1/Accounts/user/Sms/send
at com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:120)
at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:43)
at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.fetchResponse(URLFetchServiceStreamHandler.java:417)
at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getInputStream(URLFetchServiceStreamHandler.java:296)
我的代码:
String urlString =
"https://user:passtoken@twilix.exotel.in/v1/Accounts/user/Sms/send";
URL url = new URL(urlString.toString());
String userpass = "user:passtoken";
String basicAuth = "Basic " +
javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty ("Authorization", basicAuth);
urlConn.connect();
responseCode = urlConn.getResponseCode(); //Throws Exception
当我尝试将我的网址设为https://httpbin.org/post
时,一切正常这是GAE的错误还是我的代码有问题?
答案 0 :(得分:2)
看起来问题在于&符号。由于您已将凭据放在HTTP请求中,因此URL中也不需要它们。请尝试将第一行更改为此。
String urlString = "https://twilix.exotel.in/v1/Accounts/user/Sms/send";