请教关于HttpWebRequest Post数据到远程服务器的问题

jasonlee0927 2015-04-11 07:42:12
我用HttpWebRequest里用Post方法将数据Post到一个一般处理程序MaxScriptHandler.ashx,在测试的时候发现如果将MaxScriptHandler.ashx发布到内部服务器上(如192.168.0.255)时,Post数据到MaxScriptHandler.ashx上就会报错:未将对象引用为实例。
而不发布到服务器上,在本机上的开发环境测试又没有任何问题。
MaxscriptHandler.ashx代码:

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);
}

Demo.aspx页面调用:


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;
}


HttpNetHelpr代码:

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; }
}

...全文
202 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
阳澄天天 2015-04-14
  • 打赏
  • 举报
回复
引用 3 楼 jasonlee0927 的回复:
[quote=引用 1 楼 sp1234 的回复:] 贴出你的 192.168.0.255 上显示的错误页来。难道你懒得去看看人家告诉你错误所在行(行号、源代码、上下文)等信息吗?
报的错误如下: Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] T3FX.PM.Web.Handler.MaxScriptHandler.LoadScript(HttpContext context) +634 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +912 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +164[/quote] 如果 T3FX是第三方的框架就查查配置,如果是你们自己开发的就查查代码。
  • 打赏
  • 举报
回复
跨域post问题?
  • 打赏
  • 举报
回复
引用 3 楼 jasonlee0927 的回复:
[NullReferenceException: Object reference not set to an instance of an object.] T3FX.PM.Web.Handler.MaxScriptHandler.LoadScript(HttpContext context) +634 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +912 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +164
查找你的代码呗。你不是声明为异步页面了嘛。如果不是你配置的,就是别人给你使坏。
  • 打赏
  • 举报
回复
引用 3 楼 jasonlee0927 的回复:
[NullReferenceException: Object reference not set to an instance of an object.] T3FX.PM.Web.Handler.MaxScriptHandler.LoadScript(HttpContext context) +634 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +912 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +164
查找你的代码呗。你不是声明为异步页面了嘛。如果不是你配置的,就是别人给你使坏。
jasonlee0927 2015-04-12
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
贴出你的 192.168.0.255 上显示的错误页来。难道你懒得去看看人家告诉你错误所在行(行号、源代码、上下文)等信息吗?
报的错误如下: Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] T3FX.PM.Web.Handler.MaxScriptHandler.LoadScript(HttpContext context) +634 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +912 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +164
  • 打赏
  • 举报
回复
不清不楚……是哪里报了这个错?
  • 打赏
  • 举报
回复
贴出你的 192.168.0.255 上显示的错误页来。难道你懒得去看看人家告诉你错误所在行(行号、源代码、上下文)等信息吗?

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧