...
con.setRequestProperty( "Content-Type","application/x-www-form-urlencoded" );
//这样加属性也是不行
...
String para = "Title=中国"+"&Data=人民";
//URLEncoder.encode(para)也不行
...
con.setDoOutput( true );
OutputStreamWriter out = new OutputStreamWriter( con.getOutputStream() );
out.write( para );
out.flush();
请大侠指点一下!
谢谢!
------解决方案--------------------
public boolean SendDXJL(String called, String content) {
String baseurl = configInit.baseUrl();
NameValuePair pra1=new NameValuePair("called",called);
NameValuePair pra2=new NameValuePair("content",content);
NameValuePair[] data ={pra1,pra2};
System.out.println("****************"+baseurl);
// operationState = "成功";
HttpClient client = new HttpClient();
PostMethod method = new UTF8PostMethod(baseurl);
method.setRequestBody(data);
//使用POST方法
try {
client.executeMethod(method);
//取得返回信息
method.getStatusLine();
byte[] responseBody = null;
InputStream in = method.getResponseBodyAsStream();
if (in != null) {
byte[] tmp = new byte[4096];
int bytesRead = 0;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
while ((bytesRead = in.read(tmp)) != -1) {
buffer.write(tmp, 0, bytesRead);
}
responseBody = buffer.toByteArray();
}
String response = new String(responseBody, "GBK");
System.out.println(response);
//发送成功
if (response.indexOf("成功")!= -1) {
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
//释放连接
method.releaseConnection();
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SmsService s=new SmsService();
s.executeSms();
}
//重写PostMethod
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "gb2312";
}
}
------解决方案--------------------
发送前设置编码格式UTF-8,再encode()
------解决方案--------------------
发送之前设置httprequest请求的头编码为UTF-8。
------解决方案--------------------
用URLEncoder只能对参数部分encode,参数名部分不需要encode,也就是对红色部分encode。
String para = "Title=中国"+"&Data=人民";