HTTPClient 获取方法()
HTTPClient GetMethod()
我是 Apache HTTPClient 的新手,想快速了解某行代码的含义。我正在浏览 SO 并找到了这个示例代码,并且正在阅读文档以了解如何在我自己的项目中实现。以下代码来自
package http.demo;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
method = getPostMethod();
client.executeMethod(method);
System.out.println(method.getStatusLine());
Stringresponse=newString(method.getResponseBodyAsString().getBytes("8859_1"));
System.out.println(response);
method.releaseConnection();
}
private static HttpMethod getGetMethod(){
return new GetMethod("/simcard.php?simcard=1330227");
}
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod( "/simcard.php" );
NameValuePair simcard = new NameValuePair( "simcard" , "1330227" );
post.setRequestBody( new NameValuePair[] { simcard});
return post;
}
}
有人可以通俗地解释一下 simcard 行的确切含义以及它们与 getGetMethod 和 getPostMethod 的关系(这是最后两个代码块)。
谢谢。
这些行使用服务器
的基础 URL 实例化一个 HttpClient
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
然后是从该 HTTP 服务器访问特定资源。
method = getPostMethod();
client.executeMethod(method);
下一行实际上是使用 sub url
创建 GetMethod 对象
new GetMethod("/simcard.php?simcard=1330227");
所以看到完全就像向 -
发出 get 请求
www.imobile.com.cn/simcard.php?simcard=1330227
我是 Apache HTTPClient 的新手,想快速了解某行代码的含义。我正在浏览 SO 并找到了这个示例代码,并且正在阅读文档以了解如何在我自己的项目中实现。以下代码来自
package http.demo;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
method = getPostMethod();
client.executeMethod(method);
System.out.println(method.getStatusLine());
Stringresponse=newString(method.getResponseBodyAsString().getBytes("8859_1"));
System.out.println(response);
method.releaseConnection();
}
private static HttpMethod getGetMethod(){
return new GetMethod("/simcard.php?simcard=1330227");
}
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod( "/simcard.php" );
NameValuePair simcard = new NameValuePair( "simcard" , "1330227" );
post.setRequestBody( new NameValuePair[] { simcard});
return post;
}
}
有人可以通俗地解释一下 simcard 行的确切含义以及它们与 getGetMethod 和 getPostMethod 的关系(这是最后两个代码块)。
谢谢。
这些行使用服务器
的基础 URL 实例化一个 HttpClientHttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
然后是从该 HTTP 服务器访问特定资源。
method = getPostMethod();
client.executeMethod(method);
下一行实际上是使用 sub url
new GetMethod("/simcard.php?simcard=1330227");
所以看到完全就像向 -
发出 get 请求www.imobile.com.cn/simcard.php?simcard=1330227