java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.visitor.repository.getURLContent.getURLByPost(getURLContent.java:72)
at com.visitor.repository.getURLContent.main(getURLContent.java:102)
遇到这种异常,有很多种情况,我先说说我的处理方法,我是因为用的post方法传递的参数有错,所有报这个错,先看看我的错
`String str = “{”+"“id”:"+user.getUserId()+","+"“name”:"+"""+user.getUsername()+"""+","+"“idcardNum”:"
+"""+user.getCarId()+"""+"}";
post传递参数正确的方式是`
String str = "pass="+"123456789"+"&"+"id="+"-1";` 这里需要注意的是参数与参数之间是有“&”分隔符隔开的。
不多说了,再放一波Java程序访问url,get和post的通用工具类
package com.weixinManage.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 连接设备url
* @author Administrator
*
*/
public class HttpContent {
/**
* post 方法访问
* @param URL
* @param paraem
* @return
* @throws MalformedURLException
*/
public static String getURLPostContent(String urls, String paraem){
String result = null;//接收输出流
//要访问的url地址
try {
URL url = new URL(urls);
//连接访问地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//因为是post,所以用httpUrl接收
//设置参数 注意连接上后要先设置参数在做其他操作
connection.addRequestProperty("encoding", "UTF-8");
connection.setDoInput(true);//向互联网读取数据,设置为true
connection.setDoOutput(true);//向互联网传递数据,设置为true
//设定使用方法 必须先设置参数在做其他
connection.setRequestMethod("POST");
//输出流 将条件输入到请求地址 注意在请求的时候一定是先写入在写出
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(paraem);//对服务器输出
bw.flush();
//获取到输入输出流 包装
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//输入
String line;
StringBuilder builder = new StringBuilder();
while((line = br.readLine()) !=null){
//填充
builder.append(line);
}
//关闭流
bw.close();
osw.close();
os.close();
br.close();
isr.close();
is.close();
result = builder.toString();
} catch (MalformedURLException e) {
System.out.println("连接失败!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("写入异常");
e.printStackTrace();
}
return result;
}
/**
* get方法访问
* @param urlStr
* @return程序中访问http数据接口
*/
public static String getURLGetContent(String urlStr) {
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/**//** 输入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
System.out.println("输入异常!");
} finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ex) {
System.out.println("输出异常!");
}
}
String result =sb.toString();
System.out.println(result);
return result;
}
}