通过java.net.URLConnection发送HTTP请求的方法,遇到返回的是html/text格式,本

J2EE 码拜 9年前 (2016-03-14) 1504次浏览
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class gerenrz {
public static void main(String[] args) throws Exception {
System.out.println(doPost(“HA”, “49D762812B8E49437FD3443B8F0CE4BC”, 0, “7150020160010634”, “0001”, “20160601”, “20160622”, 1, 20));
}
public static String doPost(String OrigDomain, String Token,Integer RequestType,String CardNo,String AcctType,String BeginDate,String EndDate,Integer PageNum,Integer PageSize) throws Exception {
String parameterData = “{“OrigDomain”:”” + OrigDomain + “”,”Token”:””+Token+””,”RequestType”:””+ RequestType+””,”CardNo”:”” + CardNo+””,”AcctType”:”” +AcctType+””,”BeginDate”:”” +BeginDate+””,”EndDate”:””+EndDate+””,”PageNum”:””+PageNum+””,”PageSize”:””+PageSize+””}”;
URL localURL = new URL(“http://10.0.4.107:5500/v1/account/AccBookQuery”);
URLConnection connection = localURL.openConnection();

HttpURLConnection httpurlconnection = (HttpURLConnection)connection;
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod(“POST”);
httpurlconnection.setRequestProperty(“Accept-Charset”, “utf-8”);
httpurlconnection.setRequestProperty(“content-type”, “application/x-www-form-urlencoded;charset=UTF-8”);

OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;

try {
outputStream = httpurlconnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);

outputStreamWriter.write(parameterData.toString());
outputStreamWriter.flush();

if (httpurlconnection.getResponseCode() >= 300) {
throw new Exception(“HTTP Request is not success, Response code is ” + httpurlconnection.getResponseCode());
}

inputStream = httpurlconnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);

while ((tempLine = reader.readLine()) != null) {
StringBuffer aaa=  resultBuffer.append(tempLine);

}

} finally {

if (outputStreamWriter != null) {
outputStreamWriter.close();
}

if (outputStream != null) {
outputStream.close();
}

if (reader != null) {
reader.close();
}

if (inputStreamReader != null) {
inputStreamReader.close();
}

if (inputStream != null) {
inputStream.close();
}

}
return resultBuffer.toString();
}
}
本人试试之后遇到一个问题
当  httpurlconnection.setRequestProperty(“content-type”, “application/json”); 返回的是html/text格式的东西
当 httpurlconnection.setRequestProperty(“content-type”, “application/x-www-form-urlencoded;charset=UTF-8”); 就会报应用域代码错误
百度了很久 无法解决,求高手!

解决方案

40

URL localURL = new URL(“http://10.0.4.107:5500/v1/account/AccBookQuery”);
改成
URL localURL = new URL(“http://10.0.4.107:5500/v1/account/AccBookQuery?format=json”);
试试

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明通过java.net.URLConnection发送HTTP请求的方法,遇到返回的是html/text格式,本
喜欢 (1)
[1034331897@qq.com]
分享 (0)