public void TryConnectByHttpRequest()
??????? {
??????????? HttpWebRequest httpReq = null;
??????????? try
??????????? {
??????????????? if (!string.IsNullOrEmpty(this.servUrl))
??????????????? {
??????????????????? string param = "test1=zh-CN&test2=request";
??????????????????? byte[] data = Encoding.ASCII.GetBytes(param);
?
??????????????????? httpReq = (HttpWebRequest)HttpWebRequest.Create(this.servUrl);
??????????????????? httpReq.Method = "POST";//不用Post用Get某些Http服務(wù)會(huì)報(bào)錯(cuò):遠(yuǎn)程服務(wù)器返回錯(cuò)誤: (411) 所需的長(zhǎng)度。
??????????????????? httpReq.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
??????????????????? httpReq.ContentLength = data.Length;
??????????????????? //忽略緩存,完全使用服務(wù)器滿(mǎn)足請(qǐng)求
??????????????????? httpReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
??????????????????? //響應(yīng)延時(shí)為4S
??????????????????? httpReq.Timeout = 4000;
??????????????????? //這個(gè)在Post的時(shí)候,一定要加上,如果服務(wù)器返回錯(cuò)誤,他還會(huì)繼續(xù)再去請(qǐng)求,不會(huì)使用之前的錯(cuò)誤數(shù)據(jù),做返回?cái)?shù)據(jù)
??????????????????? httpReq.ServicePoint.Expect100Continue = false;
??????????????????? httpReq.ServicePoint.ConnectionLimit = 500;
?
??????????????????? using (Stream reqStream = httpReq.GetRequestStream())
??????????????????? {
??????????????????????? reqStream.Write(data, 0, data.Length);
??????????????????? }
?
??????????????????? IAsyncResult result = httpReq.BeginGetResponse(new AsyncCallback(RespCallback), httpReq);
?
??????????????????? int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout
??????????????????? ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), httpReq, DefaultTimeout, true);
??????????????? }
??????????? }
??????????? catch (WebException ex)
??????????? {
??????????????? FrmMain.Instance.WriteClientLog("嘗試連接Web服務(wù)出現(xiàn)異常:" + ex.Message);
??????????? }
??????? }
?
??????? public void TryConnectByHttpRequestGet()
??????? {
??????????? HttpWebRequest httpReq = null;
??????????? try
??????????? {
??????????????? if (!string.IsNullOrEmpty(this.servUrl))
??????????????? {
??????????????????? httpReq = (HttpWebRequest)HttpWebRequest.Create(this.servUrl);
??????????????????? httpReq.Method = "GET";
??????????????????? httpReq.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
??????????????????? //忽略緩存,完全使用服務(wù)器滿(mǎn)足請(qǐng)求
??????????????????? httpReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
??????????????????? //響應(yīng)延時(shí)為4S
??????????????????? httpReq.Timeout = 4000;
?
??????????????????? IAsyncResult result = httpReq.BeginGetResponse(new AsyncCallback(RespCallback), httpReq);
?
??????????????????? int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout
??????????????????? ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), httpReq, DefaultTimeout, true);
??????????????? }
??????????? }
??????????? catch (WebException ex)
??????????? {
??????????????? FrmMain.Instance.WriteClientLog("嘗試連接Web服務(wù)出現(xiàn)異常:" + ex.Message);
??????????? }
??????? }
?
上面寫(xiě)的兩個(gè)方法分別是用的Post 和Get請(qǐng)求!
?
客戶(hù)公司提供了HTTP服務(wù)
我用Post方法正常連通 Get方法異常:遠(yuǎn)程服務(wù)器返回錯(cuò)誤: (411) 所需的長(zhǎng)度。
我自己用HttpListener寫(xiě)的Http服務(wù)
我用Post方法異常:操作超時(shí) Get方法正常連通。
?
為啥會(huì)出現(xiàn)不同的情況?。窟€有一般去請(qǐng)求HTTP到底是用Get還是Http???
2 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
從名稱(chēng)上看,大致上,我們通常這樣分:如果是從服務(wù)器下載數(shù)據(jù),一般用GET(獲取的意思),如果是從客戶(hù)端提交數(shù)據(jù),一般用POST(提交的意思)
當(dāng)然,這兩者并沒(méi)有明確的界限。獲取數(shù)據(jù)也可以用POST,提交數(shù)據(jù)也可以用GET(如果提交的數(shù)據(jù)為簡(jiǎn)單數(shù)據(jù)的話)

揚(yáng)帆大魚(yú)
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
你可要從服務(wù)上獲取數(shù)據(jù),使用get方法,要制定獲取數(shù)據(jù)的長(zhǎng)度,要不然服務(wù)器不知道你要獲取多少。
- 2 回答
- 0 關(guān)注
- 686 瀏覽
添加回答
舉報(bào)
0/150
提交
取消