1
This commit is contained in:
61
Gatedge.ScanCode/Common/HttpClients.cs
Normal file
61
Gatedge.ScanCode/Common/HttpClients.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace Gatedge.ScanCode.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// http提交客户端
|
||||
/// </summary>
|
||||
public class HttpClients
|
||||
{
|
||||
/// <summary>
|
||||
/// Seivice URL
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
/// <summary>
|
||||
/// 内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// Cookie,保证登录后,所有访问持有一个Cookie;
|
||||
/// </summary>
|
||||
static CookieContainer Cookie = new CookieContainer();
|
||||
|
||||
/// <summary>
|
||||
/// HTTP访问
|
||||
/// </summary>
|
||||
public string Post()
|
||||
{
|
||||
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
|
||||
httpRequest.Method = "POST";
|
||||
httpRequest.ContentType = "application/json";
|
||||
httpRequest.CookieContainer = Cookie;
|
||||
httpRequest.Timeout = 1000 * 60; //1min
|
||||
|
||||
using (Stream reqStream = httpRequest.GetRequestStream())
|
||||
{
|
||||
|
||||
string sContent = string.Empty;
|
||||
var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);
|
||||
reqStream.Write(bytes, 0, bytes.Length);
|
||||
reqStream.Flush();
|
||||
}
|
||||
using (var repStream = httpRequest.GetResponse().GetResponseStream())
|
||||
{
|
||||
using (var reader = new StreamReader(repStream))
|
||||
{
|
||||
return ValidateResult(reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
}
|
||||
private static string ValidateResult(string responseText)
|
||||
{
|
||||
if (responseText.StartsWith("response_error:"))
|
||||
{
|
||||
return responseText.TrimStart("response_error:".ToCharArray());
|
||||
}
|
||||
return responseText;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user