首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >httpsurlconnection在java应用程序中错误地发布字符串

httpsurlconnection在java应用程序中错误地发布字符串
EN

Stack Overflow用户
提问于 2014-03-29 09:12:54
回答 2查看 236关注 0票数 0

在我的java应用程序中,我使用Httpsurlconnection将一些字符串数据发送到服务器。当我在android上测试这段代码时,它工作得很好。然而,在java应用程序中,它不起作用。客户端java应用如下:

代码语言:javascript
运行
复制
public static void main(String[] args) {

    disableSslVerification();
    new HttpsClient().testIt();
}

private void testIt() {

    String https_url = "https://XXX.XX.XXX.XXX:XXXX/XXXXX/TestServlet";
    URL url;
    try {

        url = new URL(https_url);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        print_content(con, "test");

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void print_content(HttpsURLConnection connection, String data) {
    if (connection != null) {

        try {
            connection.setConnectTimeout(6000);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            Charset cSet = Charset.forName("UTF-8");
            byte bytes[] = data.getBytes(cSet);
            connection.setRequestProperty("Content-Length", ""
                    + Integer.toString(bytes.length));
            connection.setRequestProperty("Content-Language", "tr");
            connection.setRequestProperty("Accept-Charset", "UTF-8");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());

            wr.write(bytes);
            wr.flush();
            wr.close();

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, cSet));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append(System.getProperty("line.separator"));
            }
            rd.close();
            System.out.println(response.toString());

        } catch (Exception e) {
            System.out.println(e.getMessage());

        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

    }

}

servlet如下所示:

代码语言:javascript
运行
复制
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String s = getHTML(request);
    try {
        out.print("received data:");
        out.print(s);
    } finally {            
        out.close();
    }
}

private String getHTML(HttpServletRequest request) throws IOException {
    int n = request.getContentLength();
    if (n < 1) {
        return "";
    }

    byte bytes[] = new byte[n];
    request.getInputStream().read(bytes);
    return new String(bytes, "UTF-8");
}

当我运行这个应用程序时,servlet的响应是:

接收数据:t☐☐☐

总是只有第一个字符被正确地发送到servlet。同样的代码在android上也能完美运行。有谁能帮帮我吗?谢谢..。

EN

Stack Overflow用户

发布于 2014-03-29 09:24:32

代码语言:javascript
运行
复制
request.getInputStream().read(bytes);

您可能需要在循环中执行读取操作。至少,检查已经读取了多少字节。除了第一个字符之外,数组看起来是空的。

从输入流中读取一定数量的字节,并将它们存储到缓冲区数组b中。实际读取的字节数以整数形式返回。此方法会一直阻塞,直到输入数据可用、检测到文件结尾或引发异常。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22725750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档