日期:2014-05-17  浏览次数:20757 次

HttpClient 的问题 在java 下编译出错。
我是新手,所以一边看代码,一边编译,加深理解,结果,一编译就报错,
但又不是语法错误,求高手给解释一下。
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicHttpResponse;
public class Client {
static URI uri;
/**
* @param args
* @throws IOException 
* @throws ClientProtocolException 
* @throws URISyntaxException 
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());


}

}











Exception in thread "main" java.lang.RuntimeException: Stub!
at org.apache.http.message.AbstractHttpMessage.<init>(AbstractHttpMessage.java:6)
at org.apache.http.message.BasicHttpResponse.<init>(BasicHttpResponse.java:8)
at Client.main(Client.java:20)


------解决方案--------------------
异常没捕捉,要么捕捉,要么在main方法上抛出
建议写class时用eclipse,这样自动会给你提示的
------解决方案--------------------
里面一定有会产生异常的方法,抛异常throws +异常类型或者try catch,我还有一个疑问,你怎么没连接URL。给你个简单参考的
Java code
package http.demo;

import java.io.IOException;
//http://passport.csdn.net/account/login?from=http%3A%2F%2Fhi.csdn.net%3F%26t%3D1317216244
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class Httpclient {
public static void main(String[] args) throws IOException
{
    HttpClient httpclient=new HttpClient();
    HttpMethod method=new GetMethod("http://baidu.com");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    try {
        int statusCode=httpclient.executeMethod(method);
        if(statusCode!=HttpStatus.SC_OK);
        {
            System.err.println("Method failed: "+method.getStatusLine());
        }
        byte[] responseBody=method.getResponseBody();
        System.out.println(new String(responseBody));
    } catch (HttpException e) {
        // TODO Auto-generated catch block
         System.out.println("Please check your provided http address!");

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    method.releaseConnection();
}
}

------解决方案--------------------
public class HttpConnect {
public static String readContentFromGet(String getUrlString, String param,