java http 400,java.io.IOException:服务器返回HTTP响应代码:400表示URL

I am working on a thread java application that hitting a url to send sms messages

the problem is i am behind an NTLM proxy server and i have searched most of the day and tried many solutions but no success the application give the titled error and when i tried to print the error response i have found that its an error page comes from the proxy server

this is the hitting code

System.setProperty("java.net.useSystemProxies", "true");

System.setProperty("http.proxyHost", AUTH_PROXY");

System.setProperty("http.proxyPort", AUTH_PROXY_PORT);

System.setProperty("http.proxyUser", AUTH_USER);

System.setProperty("http.proxyPassword", AUTH_PASSWORD);

Authenticator.setDefault(

new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(AUTH_USER, AUTH_PASSWORD.toCharArray());

}

}

);

URL url = new URL(urlString);

HttpURLConnection httpConn =(HttpURLConnection)url.openConnection();

httpConn.setReadTimeout(10000);

String resp = getResponse(httpConn);

logger.info("urlString=" + urlString);

logger.info("Response=" + resp);

here i get the respoonse

private String getResponse(HttpURLConnection Conn) throws IOException {

InputStream is;

if (Conn.getResponseCode() >= 400) {

is = Conn.getErrorStream();

} else {

is = Conn.getInputStream();

}

String response = "";

byte buff[] = new byte[512];

int b = 0;

while ((b = is.read(buff, 0, buff.length)) != -1) {

response += new String(buff, 0, b);

}

is.close();

return response;

}

any help is appreciated thanks

解决方案

After many tries i have realized that the code above is fine and the 400 error that i was getting is from not encoding the URL parameters that could have spaces

just used

URLEncoder.encode(urlParameter,"UTF-8");

for parameters that code have spaces solved the problem

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值