我想对http://quiz.uhoop.tom.com/User_Ask.php这个网站,用httpclient模拟访问,联系访问前2道答题,其中,第一次答题我可以用httpclient成功打开页面,并且把内容都打印出来,但是第二次如果访问???我除了需要设置post的参数,还需要设置什么?????新手,对http不太了解,希望可以得到大家的帮助~~~~最好讲的细致点~~~~~
下面是我写的访问第一道题的程序:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;public class tsethttp { static String url1 = "http://quiz.uhoop.tom.com/User_Ask.php";
/**
* @param args
*/
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
/*
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("end", ""));
formparams.add(new BasicNameValuePair("option", "7474"));
formparams.add(new BasicNameValuePair("question_id", "1869"));
formparams.add(new BasicNameValuePair("seconds", "29"));
formparams.add(new BasicNameValuePair("time", "23"));
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, "UTF-8");
*/
// HttpPost httppost = new HttpPost("http://quiz.uhoop.tom.com/User_Ask.php");
HttpGet httpget = new HttpGet("http://quiz.uhoop.tom.com/User_Ask.php");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength()); //start 读取整个页面内容
InputStream is = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line+"\n");
}
//end 读取整个页面内容
System.out.println(response.getAllHeaders()); System.out.println(buffer.toString());
System.out.println("----------------------------------------");
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort(); // When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}}
下面是我写的访问第一道题的程序:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;public class tsethttp { static String url1 = "http://quiz.uhoop.tom.com/User_Ask.php";
/**
* @param args
*/
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
/*
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("end", ""));
formparams.add(new BasicNameValuePair("option", "7474"));
formparams.add(new BasicNameValuePair("question_id", "1869"));
formparams.add(new BasicNameValuePair("seconds", "29"));
formparams.add(new BasicNameValuePair("time", "23"));
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, "UTF-8");
*/
// HttpPost httppost = new HttpPost("http://quiz.uhoop.tom.com/User_Ask.php");
HttpGet httpget = new HttpGet("http://quiz.uhoop.tom.com/User_Ask.php");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength()); //start 读取整个页面内容
InputStream is = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line+"\n");
}
//end 读取整个页面内容
System.out.println(response.getAllHeaders()); System.out.println(buffer.toString());
System.out.println("----------------------------------------");
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort(); // When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货