0

I tried the code below:

public class URLUploader {

    public static void main(String[] args) throws IOException 
    {

        URL url = new URL("http://77.203.65.164:6011");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);

        String name = "user";
                String password = "password";

                String authString = name + ":" + password;
                System.out.println("auth string: " + authString);
                byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
                String authStringEnc = new String(authEncBytes);
                System.out.println("Base64 encoded auth string: " + authStringEnc);

        conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write("/var/www/html/kannel/javacode/13569595024298.xml");
        writer.flush();
        String line;
        BufferedReader reader = new BufferedReader(new    InputStreamReader(conn.getInputStream()));
        while ((line = reader.readLine()) != null) {
          System.out.println(line);
        }

        writer.close();
        reader.close();
    }
}

But I got the following error:

auth string: optiweb:optiweb
Base64 encoded auth string: b3B0aXdlYjpvcHRpd2Vi
    Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: 77.203.65.164:6011
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
    at URLUploader.main(URLUploader.java:32)

What could be wrong?

4

1 回答 1

0

首先,HTTP 响应代码 500 是“内部服务器错误”,与身份验证无关。

二、声明

writer.write("/var/www/html/kannel/javacode/13569595024298.xml");

只是将文件的完整路径名写入服务器,而不是实际的文件内容,甚至没有尾随的换行符。这当然不是服务器所期望的,并且可能是 500 响应的原因。您正在构建和发送的请求也可能存在其他问题,但是如果没有详细的 API 参考来了解连接另一端的任何内容,则很难提供进一步的帮助。

于 2013-01-09T05:02:44.400 回答