java程序访问URL,get和post方法链接异常

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;  
         }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值