HttpGet HttpPost 的基本用法
2011-09-07 06:54 cjzhang 阅读(1328) 评论(0) 编辑 收藏 举报public interface
HttpRequest
implements HttpMessage
org.apache.http.HttpRequest
BasicHttpEntityEnclosingRequest, BasicHttpRequest, EntityEnclosingRequestWrapper, HttpDelete, HttpEntityEnclosingRequest, HttpEntityEnclosingRequestBase,HttpGet, HttpHead, HttpOptions, HttpPost, HttpPut, HttpRequestBase, HttpTrace, HttpUriRequest, RequestWrapper
public class
HttpGet
extends HttpRequestBase ↳
org.apache.http.message.AbstractHttpMessage
↳
org.apache.http.client.methods.HttpRequestBase
↳
org.apache.http.client.methods.HttpGet
使用步骤:
Post: 1、 声明实例化HttpPost HttpPost httpRequest=new HttpPost(URI)
2、 用NameValuePair[]数组储存Post要运行的变量
List<NameValuePair > parms=new ArrayList<NameValuePair>
parms.add(new BasicNameValuePair(“str”,"I am Post String”));
3、发出httpRequest
httprequest.setEntity(new UrlEncodedFormEntity(parms, HTTP.UTF_8));
4、返回httprespose
HttpResponse httpresponse=new DefaultHttpClient().execute(httprequest);
5、根据实际网络反馈 情况反映信息 取出httpResponse的信息
if(httpResponse.getStatusLine().getStatusCode()==200)
String strResult=EntityUtils.toString(httpresponse.getEntity());
Get
1、实例化HttpGet
HttpGet httpRequest =new HttpGet(Uri);
2、返回httpResponse
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
3、
根据实际网络反馈 情况反映信息 取出httpResponse的信息
if(httpResponse.getStatusLine().getStatusCode()==200)
String strResult=EntityUtils.toString(httpresponse.getEntity());
strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);
public String eregi_replace(String strFrom, String strTo, String strTarget)
{
String strPattern = "(?i)"+strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if(m.find())
{
return strTarget.replaceAll(strFrom, strTo);
}
else
{
return strTarget;
}
}