软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

208 lines
7.9 KiB

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace WebApiTools.Handle
{
/// <summary>
/// WebApiCommon 的摘要说明
/// </summary>
public class WebApiCommon : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string action = context.Request.Params["Action"];
string returnstr = "";
switch (action)
{
case "getParam":
returnstr = getParam(context);
break;
case "postParam":
returnstr = postParam(context);
break;
case "postParamHead":
returnstr = postParamHead(context);
break;
case "getParamHead":
returnstr = getParamHead(context);
break;
case "postBodyHead":
returnstr = postBodyHead(context);
break;
}
context.Response.Write(returnstr);
}
private string getParam(HttpContext context)
{
string ret = string.Empty;
try
{
string getUrl = context.Request.Params["getUrl"];
string paramStr = context.Request.Params["paramStr"];
byte[] byteArray = Encoding.UTF8.GetBytes(paramStr); //转化
WebRequest webReq = WebRequest.Create(new Uri(getUrl + "?" + paramStr));
webReq.Method = "GET";
webReq.ContentType = "application/x-www-form-urlencoded";
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
ret = "{\"code\":500,\"success\":false,\"msg\":\"调用同步接口异常\",\"error\":\"" + str + "\"}";
}
return ret;
}
private string postParam(HttpContext context)
{
string ret = string.Empty;
try
{
string postUrl = context.Request.Params["postUrl"];
string paramStr = context.Request.Params["paramStr"];
byte[] byteArray = Encoding.UTF8.GetBytes(paramStr); //转化
WebRequest webReq = WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
ret = "{\"code\":500,\"success\":false,\"msg\":\"调用同步接口异常\",\"error\":\""+ str + "\"}";
}
return ret;
}
private string postParamHead(HttpContext context)
{
string ret = string.Empty;
try
{
string postUrl = context.Request.Params["postUrl"];
string authorization = context.Request.Params["authorization"];
string paramStr = context.Request.Params["paramStr"];
byte[] byteArray = Encoding.UTF8.GetBytes(paramStr); //转化
WebRequest webReq = WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Headers.Add("Authorization", authorization);
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
ret = "{\"code\":500,\"success\":false,\"msg\":\"调用同步接口异常\",\"error\":\"" + str + "\"}";
}
return ret;
}
private string getParamHead(HttpContext context)
{
string ret = string.Empty;
try
{
string getUrl = context.Request.Params["getUrl"];
string contentType = context.Request.Params["contentType"];
string authorization = context.Request.Params["authorization"];
string paramStr = context.Request.Params["paramStr"];
byte[] byteArray = Encoding.UTF8.GetBytes(paramStr); //转化
WebRequest webReq = WebRequest.Create(new Uri(getUrl + "?" + paramStr));
webReq.Method = "GET";
webReq.ContentType = contentType;
webReq.Headers.Add("Authorization", authorization);
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
ret = "{\"code\":500,\"success\":false,\"msg\":\"调用同步接口异常\",\"error\":\"" + str + "\"}";
}
return ret;
}
private string postBodyHead(HttpContext context)
{
string ret = string.Empty;
try
{
string postUrl = context.Request.Params["postUrl"];
string contentType = context.Request.Params["contentType"];
string authorization = context.Request.Params["authorization"];
string bodyStr = context.Request.Params["bodyStr"];
byte[] byteArray = Encoding.UTF8.GetBytes(bodyStr); //转化
WebRequest webReq = WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = contentType;
webReq.Headers.Add("Authorization", authorization);
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
ret = "{\"code\":500,\"success\":false,\"msg\":\"调用同步接口异常\",\"error\":\"" + str + "\"}";
}
return ret;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}