我是靠谱客的博主 耍酷奇异果,最近开发中收集的这篇文章主要介绍HttpClient使用Post和Get提交参数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package httpclient;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpClientTest {

	public static void main(String[] args) throws Exception{
		String url = "/webservices/DomesticAirline.asmx/getDomesticAirlinesTime";
		String host = "www.webxml.com.cn";
		String param = "startCity="+URLEncoder.encode("杭州", "utf-8")+"&lastCity=&theDate=&userID=";
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host, 80, "http");		
		
		HttpMethod method = getMethod(url, param);
		//HttpMethod method = postMethod(url);
		
		httpClient.executeMethod(method);
		
		String response = method.getResponseBodyAsString();
		//String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));				
		System.out.println(response);
	}
	
	private static HttpMethod getMethod(String url,String param) throws IOException{
		GetMethod get = new GetMethod(url+"?"+param);
		get.releaseConnection();
		return get;
	}
		
	private static HttpMethod postMethod(String url) throws IOException{ 
		PostMethod post = new PostMethod(url);
		post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");  
		NameValuePair[] param = { new NameValuePair("startCity","杭州"),
				new NameValuePair("lastCity","沈阳"),
				new NameValuePair("userID",""),
				new NameValuePair("theDate","")	} ;
    	post.setRequestBody(param);
    	post.releaseConnection();
		return post;
	}
}




如果PostMethod提交的是中文字符,需要加上相应的编码格式:
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk"); 

如果GetMethod提交的参数有中文字符,需要先转换成utf-8格式:
URLEncoder.encode("杭州", "utf-8");


运行或结果如下:
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://WebXml.com.cn/">
  <xs:schema id="Airlines" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Airlines" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="AirlinesTime">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Company" type="xs:string" minOccurs="0" />
                <xs:element name="AirlineCode" type="xs:string" minOccurs="0" />
                <xs:element name="StartDrome" type="xs:string" minOccurs="0" />
                <xs:element name="ArriveDrome" type="xs:string" minOccurs="0" />
                <xs:element name="StartTime" type="xs:string" minOccurs="0" />
                <xs:element name="ArriveTime" type="xs:string" minOccurs="0" />
                <xs:element name="Mode" type="xs:string" minOccurs="0" />
                <xs:element name="AirlineStop" type="xs:string" minOccurs="0" />
                <xs:element name="Week" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <Airlines xmlns="">
      <AirlinesTime diffgr:id="AirlinesTime1" msdata:rowOrder="0">
        <Company>东方航空</Company>
        <AirlineCode>MU5131</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>07:30</StartTime>
        <ArriveTime>09:45</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime2" msdata:rowOrder="1">
        <Company>南方航空</Company>
        <AirlineCode>CZ5943</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>07:30</StartTime>
        <ArriveTime>09:45</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime3" msdata:rowOrder="2">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1701</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>07:55</StartTime>
        <ArriveTime>09:55</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime4" msdata:rowOrder="3">
        <Company>中国国航</Company>
        <AirlineCode>CA1701</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>07:55</StartTime>
        <ArriveTime>09:55</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime5" msdata:rowOrder="4">
        <Company>中国国航</Company>
        <AirlineCode>CA1703</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>09:00</StartTime>
        <ArriveTime>11:00</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime6" msdata:rowOrder="5">
        <Company>中国国航</Company>
        <AirlineCode>CA1711</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>10:00</StartTime>
        <ArriveTime>11:55</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime7" msdata:rowOrder="6">
        <Company>上海航空</Company>
        <AirlineCode>FM9157</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>10:30</StartTime>
        <ArriveTime>12:45</ArriveTime>
        <Mode>75B</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime8" msdata:rowOrder="7">
        <Company>中国国航</Company>
        <AirlineCode>CA1510</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>11:00</StartTime>
        <ArriveTime>13:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime9" msdata:rowOrder="8">
        <Company>海南航空</Company>
        <AirlineCode>HU7278</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>11:45</StartTime>
        <ArriveTime>13:55</ArriveTime>
        <Mode>738</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>345</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime10" msdata:rowOrder="9">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1596</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>12:00</StartTime>
        <ArriveTime>13:55</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime11" msdata:rowOrder="10">
        <Company>中国国航</Company>
        <AirlineCode>CA1596</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>12:00</StartTime>
        <ArriveTime>13:55</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime12" msdata:rowOrder="11">
        <Company>东方航空</Company>
        <AirlineCode>MU5167</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>12:05</StartTime>
        <ArriveTime>14:15</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime13" msdata:rowOrder="12">
        <Company>南方航空</Company>
        <AirlineCode>CZ5946</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>12:05</StartTime>
        <ArriveTime>14:15</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime14" msdata:rowOrder="13">
        <Company>中国国航</Company>
        <AirlineCode>CA1705</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>13:00</StartTime>
        <ArriveTime>14:55</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime15" msdata:rowOrder="14">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1717</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>14:00</StartTime>
        <ArriveTime>16:00</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>35日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime16" msdata:rowOrder="15">
        <Company>中国国航</Company>
        <AirlineCode>CA1717</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>14:00</StartTime>
        <ArriveTime>16:00</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>35日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime17" msdata:rowOrder="16">
        <Company>中国国航</Company>
        <AirlineCode>CA1707</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>15:00</StartTime>
        <ArriveTime>17:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime18" msdata:rowOrder="17">
        <Company>中国国航</Company>
        <AirlineCode>CA1709</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>16:00</StartTime>
        <ArriveTime>17:55</ArriveTime>
        <Mode>320</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime19" msdata:rowOrder="18">
        <Company>海南航空</Company>
        <AirlineCode>HU7156</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>16:50</StartTime>
        <ArriveTime>19:10</ArriveTime>
        <Mode>738</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime20" msdata:rowOrder="19">
        <Company>山东航空</Company>
        <AirlineCode>SC1715</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>17:00</StartTime>
        <ArriveTime>19:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime21" msdata:rowOrder="20">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1715</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>17:00</StartTime>
        <ArriveTime>19:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime22" msdata:rowOrder="21">
        <Company>中国国航</Company>
        <AirlineCode>CA1715</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>17:00</StartTime>
        <ArriveTime>19:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime23" msdata:rowOrder="22">
        <Company>上海航空</Company>
        <AirlineCode>FM9151</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>17:15</StartTime>
        <ArriveTime>19:20</ArriveTime>
        <Mode>75B</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>12356</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime24" msdata:rowOrder="23">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1564</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>18:05</StartTime>
        <ArriveTime>20:25</ArriveTime>
        <Mode>32A</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>1356日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime25" msdata:rowOrder="24">
        <Company>深圳航空</Company>
        <AirlineCode>ZH1564</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>18:05</StartTime>
        <ArriveTime>20:25</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime26" msdata:rowOrder="25">
        <Company>中国国航</Company>
        <AirlineCode>CA1564</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>18:05</StartTime>
        <ArriveTime>20:25</ArriveTime>
        <Mode>32A</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>1356日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime27" msdata:rowOrder="26">
        <Company>中国国航</Company>
        <AirlineCode>CA1564</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>18:05</StartTime>
        <ArriveTime>20:25</ArriveTime>
        <Mode>JET</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime28" msdata:rowOrder="27">
        <Company>海南航空</Company>
        <AirlineCode>HU7178</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>20:50</StartTime>
        <ArriveTime>23:10</ArriveTime>
        <Mode>738</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime29" msdata:rowOrder="28">
        <Company>中国国航</Company>
        <AirlineCode>CA1713</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>21:00</StartTime>
        <ArriveTime>23:00</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime30" msdata:rowOrder="29">
        <Company>东方航空</Company>
        <AirlineCode>MU9181</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>21:15</StartTime>
        <ArriveTime>23:15</ArriveTime>
        <Mode>75B</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime31" msdata:rowOrder="30">
        <Company>上海航空</Company>
        <AirlineCode>FM9181</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>21:15</StartTime>
        <ArriveTime>23:15</ArriveTime>
        <Mode>75B</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime32" msdata:rowOrder="31">
        <Company>厦门航空</Company>
        <AirlineCode>MF8149</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>21:50</StartTime>
        <ArriveTime>00:15</ArriveTime>
        <Mode>737</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime33" msdata:rowOrder="32">
        <Company>南方航空</Company>
        <AirlineCode>CZ6164</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>22:10</StartTime>
        <ArriveTime>00:15</ArriveTime>
        <Mode>321</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
      <AirlinesTime diffgr:id="AirlinesTime34" msdata:rowOrder="33">
        <Company>首都航空</Company>
        <AirlineCode>JD5352</AirlineCode>
        <StartDrome>杭州萧山国际机场</StartDrome>
        <ArriveDrome>北京首都国际机场</ArriveDrome>
        <StartTime>22:55</StartTime>
        <ArriveTime>01:15</ArriveTime>
        <Mode>319</Mode>
        <AirlineStop>0</AirlineStop>
        <Week>123456日</Week>
      </AirlinesTime>
    </Airlines>
  </diffgr:diffgram>
</DataSet>

原文链接:http://honda418.iteye.com/blog/337052/

最后

以上就是耍酷奇异果为你收集整理的HttpClient使用Post和Get提交参数的全部内容,希望文章能够帮你解决HttpClient使用Post和Get提交参数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部