62,074
社区成员
发帖
与我相关
我的任务
分享
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string action = context.Request.Form["action"];
Guid id = context.Request.Form["id"] == null ? Guid.Empty : new Guid(context.Request.Form["id"]);
MaxScript script = service.Get(id);
string result = script.Data;
context.Response.Write(result);
}
protected void btnReadScript_Click(object sender, EventArgs e)
{
string url = "http://192.168.0.255:8081/handler/maxscripthandler.ashx";
//如果将url改成http://localhost:1828/handler/maxscripthandler.ashx"就没有问题
string param = "{'id':'A6FAED1B-02B8-4474-B5EF-7D7DFA8AB72F','action':'load'}";
string script = HttpNetHelper.PostWebRequest(url,param);
txtScriptBody.Text = string.IsNullOrEmpty(script) ? "No Script Be Found!" : script;
}
public class HttpNetHelper
{
public static string PostWebRequest(string remoteUrl,string param)
{
string result = string.Empty;
try
{
PostData postData = new PostData();
postData = JsonConvert.DeserializeObject<PostData>(param);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("action={0}&id={1}", postData.action, postData.id);
byte[] data = Encoding.Default.GetBytes(sb.ToString());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(remoteUrl));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
reader.Close();
response.Close();
}
catch (WebException ex)
{
HttpWebResponse exResponse = ex.Response as HttpWebResponse;
StreamReader exReader = new StreamReader(exResponse.GetResponseStream());
string error = exReader.ReadToEnd();
}
return result;
}
}
internal class PostData
{
public string action { get; set; }
public Guid id { get; set; }
}