加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA Http POST请求UTF-8

发布时间:2020-12-14 23:34:54 所属栏目:Java 来源:网络整理
导读:我的J2EE应用程序能够从JSP页面接收POST请求,没问题. 但是如果我使用另一个java应用程序发送POST请求,则收到的参数不是UTF-8字符串. 这里有我的代码: URL url = new URL("http://localhost:8080/ITUNLPWebInterface/SimpleApi");HttpURLConnection cox = (H
我的J2EE应用程序能够从JSP页面接收POST请求,没问题.

但是如果我使用另一个java应用程序发送POST请求,则收到的参数不是UTF-8字符串.

这里有我的代码:

URL url = new URL("http://localhost:8080/ITUNLPWebInterface/SimpleApi");
HttpURLConnection cox = (HttpURLConnection) url.openConnection();

cox.setDoInput(true);
cox.setDoOutput(true);
cox.setRequestMethod("POST");
cox.setRequestProperty("Accept-Charset","UTF-8");
cox.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
cox.setRequestProperty("charset","UTF-8");

DataOutputStream dos = new DataOutputStream(cox.getOutputStream());
String query = "tool=ner&input=?a?a?a";
dos.writeBytes(query);
dos.close();

难道我做错了什么?

感谢您的回复

解决方法

这项工作!!!
package com.erenerdogan.utils;

import com.erenerdogan.webservice.ServiceInterface;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author erenerdogan
 */
public class WebService 
{
private String server;

    public WebService(String server) {
        this.server = server;
    }

private HttpPost createPostRequest(String method,Map<String,String> paramPairs){
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(server + "/" + method);
    // Building post parameters
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(paramPairs.size());
    for (String key : paramPairs.keySet()){
        nameValuePair.add(new BasicNameValuePair(key,paramPairs.get(key)));
            System.out.println("Key : "+ key + " - Value : "+ paramPairs.get(key) );
    }

    // Url Encoding the POST parameters
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }
    return httpPost;
}

public String callServer(String method,String> paramPairs) throws ClientProtocolException,IOException{

    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();

    HttpParams httpParameters = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters,10 * 1000);
    HttpConnectionParams.setSoTimeout(httpParameters,3 * 1000);
    HttpResponse httpResponse = httpClient.execute(createPostRequest(method,paramPairs));
    HttpEntity httpEntity = httpResponse.getEntity();
    String xml = EntityUtils.toString(httpEntity);

    return xml;
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读