下面这个方法,之前在eclipse上用一直没问题,后面不知道做了什么,这个方法就一直报错
但是我把项目放到服务器上去,就没问题,放到我同事的电脑上也没问题,所以我觉得应该是我的电脑缺少了什么东西。
有没有谁遇到过这样的情况,我开发的时候很苦恼,我现在得到的有可能解决的方法是重装系统,但是我并不想重装啊!!!
谁能帮忙解决一下,万分感激啊,这是第一次发帖子! /**
* 用form表单形式post访问接口,返回json
* @param url
* @param params
* @return response.toString()
*/
public static String formPost(String url,String params){
String responseMessage = "";
StringBuffer response = new StringBuffer();
HttpURLConnection httpConnection = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
try {
URL urlPost = new URL(url);
httpConnection = (HttpURLConnection) urlPost.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
// 参数长度太大,不能用get方式
httpConnection.setRequestMethod("POST");
// 不使用缓存
httpConnection.setUseCaches(false);
// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
httpConnection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
httpConnection.setRequestProperty("Connection", "Keep-Alive");
// 设置请求头信息
httpConnection.setRequestProperty("Charset", "UTF-8");
// 设置边界
//httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
// 实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。 httpConnection.connect();
out = new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8");
// 构建请求参数
StringBuffer sb = new StringBuffer();
sb.append(params);
out.write(sb.toString());
System.out.println("send_url:" + url);
System.out.println("send_data:" + sb.toString());
// flush and close
out.flush(); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.close();
}
if (null != reader) {
reader.close();
}
if (null != httpConnection) {
httpConnection.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
} try {
reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
while ((responseMessage = reader.readLine()) != null) {
response.append(responseMessage);
response.append("\n");
} if (!"failure".equals(response.toString())) {
System.out.println("post链接success");
} else {
System.out.println("post链接failue");
}
// 将该url的配置信息缓存起来
return response.toString();
} catch (IOException e) {
e.printStackTrace();
return response.toString();
}
}
但是我把项目放到服务器上去,就没问题,放到我同事的电脑上也没问题,所以我觉得应该是我的电脑缺少了什么东西。
有没有谁遇到过这样的情况,我开发的时候很苦恼,我现在得到的有可能解决的方法是重装系统,但是我并不想重装啊!!!
谁能帮忙解决一下,万分感激啊,这是第一次发帖子! /**
* 用form表单形式post访问接口,返回json
* @param url
* @param params
* @return response.toString()
*/
public static String formPost(String url,String params){
String responseMessage = "";
StringBuffer response = new StringBuffer();
HttpURLConnection httpConnection = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
try {
URL urlPost = new URL(url);
httpConnection = (HttpURLConnection) urlPost.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
// 参数长度太大,不能用get方式
httpConnection.setRequestMethod("POST");
// 不使用缓存
httpConnection.setUseCaches(false);
// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
httpConnection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
httpConnection.setRequestProperty("Connection", "Keep-Alive");
// 设置请求头信息
httpConnection.setRequestProperty("Charset", "UTF-8");
// 设置边界
//httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
// 实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。 httpConnection.connect();
out = new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8");
// 构建请求参数
StringBuffer sb = new StringBuffer();
sb.append(params);
out.write(sb.toString());
System.out.println("send_url:" + url);
System.out.println("send_data:" + sb.toString());
// flush and close
out.flush(); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.close();
}
if (null != reader) {
reader.close();
}
if (null != httpConnection) {
httpConnection.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
} try {
reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
while ((responseMessage = reader.readLine()) != null) {
response.append(responseMessage);
response.append("\n");
} if (!"failure".equals(response.toString())) {
System.out.println("post链接success");
} else {
System.out.println("post链接failue");
}
// 将该url的配置信息缓存起来
return response.toString();
} catch (IOException e) {
e.printStackTrace();
return response.toString();
}
}
解决方案 »
- 集思广益,人多力量大,发表想法
- Hibernate 对 MS SQL分页源码的问题
- ERP系统寻求团队或个人合作
- 在线等:myeclipse8.0安装文件双击后提示"找不到指定文件",要找什么文件啊?怎么解决?
- 如何在运行时找到JAR包中特定目录下的一些文件
- JavaWeb 项目 负载均衡的问题
- 为什么我用不了taglib
- 求java与模式ebook
- 如何才能到达J2EE设计师的水平,应该要懂那些知识???
- 靠J2EE吃饭的人-你们过得好吗?--J2EE程序员调查>>高分调查.
- 请教各位大神!怎样使用jconsole连接coherence呀!
- java.lang.ClassCastException 类型转换异常求解
这个问题重装jdk就能解决了吗?那我晚上回去试试
httpConnection.connect(); 这一句,贴出来的代码里面 的36行
我已经是jdk1.8 了,我晚上重装一下试试
private static final SSLHandler simpleVerifier = new SSLHandler();
private static SSLSocketFactory sslFactory;public static void formPost(String url,String params){
......
httpConnection.setSSLSocketFactory(getSSLSF());
httpConnection.setHostnameVerifier(simpleVerifier);
......
}
public static synchronized SSLSocketFactory getSSLSF() throws Exception
{
if(sslFactory!=null) return sslFactory;
SSLContext sc = SSLContext.getInstance("SSLv3");
sc.init(null, new TrustManager[]{simpleVerifier}, null);
sslFactory = sc.getSocketFactory();
return sslFactory;
}
private static class SSLHandler implements X509TrustManager,HostnameVerifier
{
private SSLHandler()
{
}
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
} public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return null;
} public boolean verify(String arg0, SSLSession arg1)
{
return true;
}
}
是的 https会出这样的问题,http不会谢谢你的回复,我这里之前有写过这种方法,现在也是不能用。public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new HttpsX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSLv3", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod); // 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
} // 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
} // 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}