Http GET,Post 방식 의 요청 요약
20418 단어 http
/// <summary>
/// http ,
/// </summary>
/// <param name="response">HttpWebResponse </param>
/// <returns></returns>
public static string ReaderFromHttp(HttpWebResponse response)
{
Stream stream = null;
StreamReader streamReader = null;
try
{
var contentEncoding = response.ContentEncoding; //
var characterSet = response.CharacterSet; //
stream = response.GetResponseStream();
if (contentEncoding == "gzip")
{
/* , */
throw new NotImplementedException("gzip ");
}
else
{
streamReader = new StreamReader(stream, Encoding.GetEncoding(characterSet));
return streamReader.ReadToEnd();
}
}
catch (Exception e)
{
return null;
}
finally
{
if (streamReader != null)
{
streamReader.Close();
streamReader = null;
}
if (stream != null)
{
stream.Close();
stream = null;
}
}
}
View Code
Http Get
#region Get Http
/// <summary>
/// GET HTTP
/// </summary>
/// <param name="targetUri"></param>
/// <param name="timeout"> </param>
/// <param name="cookie"> HTTP Cookie </param>
/// <returns></returns>
public static HttpWebRequest CreateGetHttpWebRequest(string targetUri, int timeout = 0, CookieCollection cookie = null)
{
HttpWebRequest request = WebRequest.Create(targetUri) as HttpWebRequest;
request.UserAgent = DefaultUserAgent;
request.Method = "GET";
if (timeout != 0)
request.Timeout = timeout;
if (cookie != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookie);
}
return request;
}
/// <summary>
/// GET HTTP ,
/// </summary>
/// <param name="targetUri"></param>
/// <param name="timeout"> </param>
/// <param name="cookie"> HTTP Cookie </param>
/// <returns></returns>
public static string ReturnFromGetHttp(string targetUri, int timeout = 0, CookieCollection cookie = null)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = CreateGetHttpWebRequest(targetUri, timeout, cookie);
response = request.GetResponse() as HttpWebResponse;
return ReaderFromHttp(response);
}
catch (Exception e)
{
return null;
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (request != null)
{
request.Abort();
request = null;
}
}
}
#endregion
View Code
Http Post
#region Post Http
/// <summary>
/// POST HTTP
/// </summary>
/// <param name="targetUri"></param>
/// <param name="content"> </param>
/// <param name="characterSet"> ( utf-8)</param>
/// <param name="timeout"> </param>
/// <param name="cookie"> HTTP Cookie </param>
/// <returns></returns>
public static HttpWebRequest CreatePostHttpWebRequest(string targetUri, string content, string characterSet = null, int timeout = 0, CookieCollection cookie = null)
{
HttpWebRequest request = WebRequest.Create(targetUri) as HttpWebRequest;
request.UserAgent = DefaultUserAgent;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
if (timeout != 0)
request.Timeout = timeout;
if (cookie != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookie);
}
using (var stream = request.GetRequestStream()) //
{
characterSet = string.IsNullOrEmpty(characterSet) ? "utf-8" : characterSet;
var bytes = Encoding.GetEncoding(characterSet).GetBytes(content);
request.ContentLength = bytes.LongLength;
stream.Write(bytes, 0, bytes.Length);
}
return request;
}
/// <summary>
/// POST HTTP ,
/// </summary>
/// <param name="targetUri"></param>
/// <param name="content"> </param>
/// <param name="characterSet"> ( utf-8)</param>
/// <param name="timeout"> </param>
/// <param name="cookie"> HTTP Cookie </param>
/// <returns></returns>
public static string ReturnFromPostHttp(string targetUri, string content, string characterSet = null, int timeout = 0, CookieCollection cookie = null)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = CreatePostHttpWebRequest(targetUri,content,characterSet, timeout, cookie);
response = request.GetResponse() as HttpWebResponse;
return ReaderFromHttp(response);
}
catch (Exception e)
{
return null;
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (request != null)
{
request.Abort();
request = null;
}
}
}
#endregion
View Code
호출 방식:
var reval = ReturnFromGetHttp("http://www.douyou100.com"); var reval1 = ReturnFromPostHttp("http://www.douyou100.com","요청 텍스트");
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
빠른 팁: SingleStoreDB의 데이터 API 사용SingleStoreDB는 HTTP 연결을 통해 SQL 문을 실행하는 데 사용할 수 있는 을 제공합니다. 이 짧은 문서에서는 이 데이터 API를 사용하는 방법에 대한 예를 보여줍니다. A는 무료 SingleStore...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.