Java中通过HTTP Post发送多行字符串

发布于 2024-12-01 01:09:25 字数 689 浏览 0 评论 0原文

我想使用 Java 向某些服务发送 POST 请求。请求必须包含一些参数,其中之一是多行字符串。我尝试了很多方法,但每次我最终都会将整个文本作为单行提交,没有换行符。有什么想法吗?

URL url = new URL("http://example.com/create");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
String data = "param1=value&string=" + convertNewLineCharacters(text);
writer.write(data);
writer.flush();

// (...)
private String convertNewLineCharacters(String text) throws Exception  {
  String encodedString = URLEncoder.encode(text, "UTF-8");
  encodedString = encodedString.replaceAll("%0A", "\r\n");

  return encodedString;
}

我仅限于标准 Java Api,因此不允许使用外部库。

I want to send post request to some service using Java. Requests must contain some parameters and one of them is multiline String. I tried many ways but everytime I end up with whole text submitted as single line without newline characters. Any ideas?

URL url = new URL("http://example.com/create");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
String data = "param1=value&string=" + convertNewLineCharacters(text);
writer.write(data);
writer.flush();

// (...)
private String convertNewLineCharacters(String text) throws Exception  {
  String encodedString = URLEncoder.encode(text, "UTF-8");
  encodedString = encodedString.replaceAll("%0A", "\r\n");

  return encodedString;
}

I am limited to standard Java Api, so no external libraries are allowed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-12-08 01:09:26
// ...
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" ); // not sure it is important
// ...
writer.write(URLEncoder.encode(data, "UTF-8")); // urlencode the whole string and do not edit it
// ...
// ...
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" ); // not sure it is important
// ...
writer.write(URLEncoder.encode(data, "UTF-8")); // urlencode the whole string and do not edit it
// ...
牵强ㄟ 2024-12-08 01:09:26
package com.foo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Foobar {
    public static void main(String...args) throws UnsupportedEncodingException {
        String text = URLEncoder.encode("Foo\nBar", "UTF-8");
        System.out.println(URLDecoder.decode(text, "UTF-8"));
    }
}

输出:

Bar

只需解码编码数据即可。 URLEncoder#encode(String s, "UTF-8") 将对保留集之外的任何字符进行百分比编码。请参阅字符数据的百分比编码。

package com.foo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Foobar {
    public static void main(String...args) throws UnsupportedEncodingException {
        String text = URLEncoder.encode("Foo\nBar", "UTF-8");
        System.out.println(URLDecoder.decode(text, "UTF-8"));
    }
}

Output:
Foo
Bar

Just decode the encoded data. URLEncoder#encode(String s, "UTF-8") will percent encode any characters outside of the reserved set. See Percent encoding of character data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文