java从url中传参中文
时间: 2024-04-23 22:22:12 浏览: 135
在Java中,我们可以使用`URLEncoder`和`URLDecoder`类编码和解码URL参数。
例如,如果我们要将一个包含中文字符的字符串作为URL参数传递,可以使用以下代码将其编码:
```java
String param = "中文参数";
String encodedParam = URLEncoder.encode(param, "UTF-8");
```
然后,我们可以将编码后的参数添加到URL中:
```java
String url = "http://example.com?param=" + encodedParam;
```
在接收方,我们可以使用以下代码解码参数:
```java
String encodedParam = request.getParameter("param");
String decodedParam = URLDecoder.decode(encodedParam, "UTF-8");
```
注意,需要使用相同的编码格式(这里是UTF-8)进行编码和解码。
相关问题
java请求posturl进行传参
可以使用 Java 的 HttpURLConnection 类来发送 POST 请求并传递参数。以下是一个示例代码:
```java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class PostRequestExample {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com/api");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
// 设置 POST 请求参数
String urlParameters = "param1=" + URLEncoder.encode("value1", "UTF-8") +
"¶m2=" + URLEncoder.encode("value2", "UTF-8");
// 发送 POST 请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// 读取响应
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
} catch (Exception e) {
System.out.println(e);
}
}
}
```
此代码将发送一个 POST 请求到 `http://example.com/api`,并传递两个参数:`param1` 和 `param2`。在发送请求时,需要将参数编码为 URL 编码。响应结果将被打印到控制台。
java 实现get请求 url传参
可以使用java中的HttpURLConnection或者HttpClient来实现get请求,同时可以通过URL传递参数。
使用HttpURLConnection实现:
```
import java.net.*;
import java.io.*;
public class HttpGetRequest {
public static void main(String[] args) {
try {
String urlStr = "http://example.com/api?param1=value1¶m2=value2";
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
使用HttpClient实现:
```
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.utils.URIBuilder;
public class HttpGetRequest {
public static void main(String[] args) {
try {
String urlStr = "http://example.com/api";
URIBuilder builder = new URIBuilder(urlStr);
builder.setParameter("param1", "value1");
builder.setParameter("param2", "value2");
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(builder.build());
request.addHeader("Accept", "application/json");
HttpResponse response = client.execute(request);
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
client.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们通过URL传递了两个参数:param1和param2。您可以根据您的需求添加更多的参数。
阅读全文