Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asmx服务中的c#.net访问原始json请求_C#_.net_Json_Proxy_Asmx - Fatal编程技术网

asmx服务中的c#.net访问原始json请求

asmx服务中的c#.net访问原始json请求,c#,.net,json,proxy,asmx,C#,.net,Json,Proxy,Asmx,我正在尝试设置一个简单的代理服务器,我们将数据发布到我的代理服务器。代理服务器将把发布的数据转发到实际服务器,并从实际服务器获得响应。然后在代理服务器上显示响应,发出请求的网站将读取该响应并对数据执行任何操作。 我在获取来自网站的原始帖子数据的第一部分遇到了麻烦。似乎asmx文件总是希望在参数之外执行操作,但我的代理只希望在原始请求上转发。它不知道参数。下面是对代理服务器的请求示例: localhost/mobile.asmx 邮递 {“用户名”:fake@email.com“,”密码“:”xx

我正在尝试设置一个简单的代理服务器,我们将数据发布到我的代理服务器。代理服务器将把发布的数据转发到实际服务器,并从实际服务器获得响应。然后在代理服务器上显示响应,发出请求的网站将读取该响应并对数据执行任何操作。 我在获取来自网站的原始帖子数据的第一部分遇到了麻烦。似乎asmx文件总是希望在参数之外执行操作,但我的代理只希望在原始请求上转发。它不知道参数。下面是对代理服务器的请求示例: localhost/mobile.asmx 邮递 {“用户名”:fake@email.com“,”密码“:”xxxx“,”应用ID“:”2302FF64-925D-4E0E-B086-73AA9FF152D8”}

再一次,我不想只得到用户名和密码。我想捕获完整的原始请求并将其转发到真正的服务器上

我试过很多东西。因为没有请求参数,所以我不能使用请求。我还认为GETUSERTOKENLOGIN函数发生在读取原始post数据流之后,因此我不能再使用流来获取数据。我试过很多东西

我希望这是一个超级简单的脚本,如果可能的话。下面是我的超级简单的例子。我知道我可以在数据周围添加一个包装器,但我不想这样做

任何帮助都将不胜感激。 MOBILE.ASMX

<%@ WebService Language="C#" Class="mobile" %>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO;
using System.Web.Script.Services;
using System.Text;
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class mobile : System.Web.Services.WebService 
{
    public mobile()
    {


    }

    // The HelloWorld() example service returns the string Hello World.
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetUserTokenLogin()
    {
        // Create a new request to the mentioned URL.
        WebRequest myWebRequest = WebRequest.Create("http://api.geonames.org/citiesJSON");
        myWebRequest.Method = "POST";
        Stream dataStream = myWebRequest.GetRequestStream();
        WebResponse myWebResponse = myWebRequest.GetResponse();

        // Print the  HTML contents of the page to the console.
        Stream streamResponse = myWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);
        String FullData = "";
        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            FullData = FullData + outputData;
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream object.
        streamResponse.Close();
        streamRead.Close();

        myWebResponse.Close();
        return FullData;
    }
}

使用制度;
使用系统集合;
使用系统组件模型;
使用系统数据;
使用系统诊断;
使用System.Web;
使用System.Web.Services;
Net系统;
使用System.IO;
使用System.Web.Script.Services;
使用系统文本;
[WebServiceBinding(ConformsTo=WsiProfiles.None)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类手机:System.Web.Services.WebService
{
公共移动电话()
{
}
//HelloWorld()示例服务返回字符串Hello World。
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串GetUserTokenLogin()
{
//创建对所述URL的新请求。
WebRequest myWebRequest=WebRequest.Create(“http://api.geonames.org/citiesJSON");
myWebRequest.Method=“POST”;
Stream dataStream=myWebRequest.GetRequestStream();
WebResponse myWebResponse=myWebRequest.GetResponse();
//将页面的HTML内容打印到控制台。
streamResponse=myWebResponse.GetResponseStream();
StreamReader streamRead=新的StreamReader(streamResponse);
Char[]readBuff=新字符[256];
int count=streamRead.Read(readBuff,0,256);
字符串FullData=“”;
而(计数>0)
{
String outputData=新字符串(readBuff,0,count);
FullData=FullData+outputData;
count=streamRead.Read(readBuff,0,256);
}
//关闭流对象。
streamResponse.Close();
streamRead.Close();
myWebResponse.Close();
返回完整数据;
}
}

解析请求是应用程序服务器的工作

代理服务器上不应包含任何应用程序代码。 如果您只想将IIS用作代理服务器,请参阅:

如果您只需要原始请求并希望写出原始响应,那么可以创建一个自定义的
HttpModule

参考:

在这个模块中,您可以在客户端发送请求时获取请求,并将其转发到另一台服务器,然后从该服务器获取响应并转发到您的客户端。(实际上,您正在制作反向代理。)


在asp.net Web服务中无法实现这一点

我最后从下面下载了这个演示:它提供了一个bin文件夹,我刚刚把它放在我的根目录中。然后我创建了一个名为mobile.asmx的文件夹。在那个文件夹中,我将演示中的webconfig放入其中,只需更改远程服务器

<appSettings>
 <add key="RemoteWebSite" value="http://my-clients-server.com/mobile_dev/" />
</appSettings>

<system.web>
  <httpHandlers>
   <add verb="*" path="*" type="ReverseProxy.ReverseProxy, ReverseProxy"/>
 </httpHandlers>
</system.web>


我用我需要的XML和JSON测试了上述内容。

要在ASMX中获取请求的原始JSON(例如,您已经从AngularJS向其发出POST请求),请尝试以下代码:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Calculate()
{
    HttpContext.Current.Request.InputStream.Position = 0;
    var jsonString = new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8).ReadToEnd();
    var json = JObject.Parse(jsonString);

    // your code
}

请注意,您不需要在此处使用
语句,因为您不是处理
InputStream

的人。是否有任何方法可以从asmx文件获取原始帖子?我正在建立文件。我希望我的客户不必经历所有这些配置。是否可以将原始邮件发送到服务,以便我可以将其转发到服务器。如果我能做到这一点,那么我的脚本就会很好地工作。我编辑了我的回复,原始帖子需要你实现
IHttpModule
。看一看,谢谢。谢谢你的时间。我发现这个解决方案非常有用:我所要做的就是下载演示,它提供了一个dll和一个网络配置。我将在我的原始问题中提供更准确的信息。Thanks@user3638471:哦,天哪!我花了几天时间找这个!!这三行代码救了我。