简体   繁体   English

如何将HttpRequest复制到另一个Web服务?

[英]How do I copy a HttpRequest to another web service?

I have two identical web services currently installed on the same PC for testing purposes. 我目前在同一台PC上安装了两个相同的Web服务用于测试目的。 A request that is received by the first service, is suposed go to the second one as well. 第一个服务接收的请求也会被转到第二个服务。 My intention was to do this using a HttpModule. 我的意图是使用HttpModule来做到这一点。 I handle the request during application.BeginRequest. 我在application.BeginRequest期间处理请求。

public void AsyncForwardRequest(HttpRequest request)
{
    try
    {
        // Prepare web request...
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(WSaddress);

        req.Credentials = CredentialCache.DefaultCredentials;
        req.Headers.Add("SOAPAction", request.Headers["SOAPAction"]);
        req.ContentType = request.Headers["Content-Type"];
        req.Accept = request.Headers["Accept"];
        req.Method = request.HttpMethod;

        // Send the data.
        long posStream = request.InputStream.Position;
        long len = request.InputStream.Length;
        byte[] buff = new byte[len];
        request.InputStream.Seek(0, SeekOrigin.Begin);
        if (len < int.MaxValue && request.InputStream.Read(buff, 0, (int)len) > 0)
        {
            using (Stream stm = req.GetRequestStream())
            {
                stm.Write(buff, 0, (int)len);
            }

            request.InputStream.Position = posStream;
            DebugOutputStream(request.InputStream);
            request.InputStream.Seek(0, SeekOrigin.Begin);

            IAsyncResult result = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(RespCallback), req);
        }
    }
    catch (Exception ex)
    {

        App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
    }

private static void RespCallback(IAsyncResult asynchronousResult)
{
    try
    {
        // State of request is asynchronous.
        var req = (HttpWebRequest)asynchronousResult.AsyncState;
        WebResponse resp = (HttpWebResponse)req.EndGetResponse(asynchronousResult);

        Stream responseStream = resp.GetResponseStream();
        System.IO.Stream stream = responseStream;
        Byte[] arr = new Byte[1024 * 100];
        stream.Read(arr, 0, 1024 * 100);
        if (arr.Length > 0)
        {
            string body = (new ASCIIEncoding()).GetString(arr);
            Debug.Print(body);
        }
    }
    catch (WebException ex)
    {
        App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
    }

}

This (HttpWebResponse)req.EndGetResponse(asynchronousResult) throws an exception: 500 Internal Server Error and fails. 此(HttpWebResponse)req.EndGetResponse(asynchronousResult)抛出异常:500内部服务器错误并失败。 The original request goes through fine. 原始请求很好。

request.InnerStream: ... request.InnerStream:...

Does this have anything to do with the web service addresses being different? 这与Web服务地址有什么不同吗? In my case they're: http://localhost/WS/service.asmx http://localhost/WS_replicate/service.asmx 在我的例子中,他们是: http://localhost/WS/service.asmx http://localhost/WS_replicate/service.asmx

I would at least implement this, to be able to read the content of a httpwebrequest that has a status 500: 我至少会实现这一点,以便能够读取状态为500的httpwebrequest的内容:

catch (WebException ex)
{
    HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
    Stream dataStream = webResponse.GetResponseStream();

    if (dataStream != null)
    {
        StreamReader reader = new StreamReader(dataStream);
        response = reader.ReadToEnd();
    }
}

Not answering your question though, i would suggest having a simple amsx that reads the request, and posts the request to the two different web services. 虽然没有回答你的问题,我建议有一个简单的amsx来读取请求,并将请求发布到两个不同的Web服务。 If you need some code for that let me know. 如果您需要一些代码,请告诉我。

Additional advantages would be that the asmx will be easier to support for the developer as well as the one dealing with the deployment. 其他优点是asmx将更容易支持开发人员以及处理部署的人员。 You could add configuration options to make the actual url's dynamic. 您可以添加配置选项以使实际URL动态化。

Was able to modify the AsyncForwardRequest method so that I can actually edit the SoapEnvelope itself: 能够修改AsyncForwardRequest方法,以便我可以实际编辑SoapEnvelope本身:

string buffString = System.Text.UTF8Encoding.UTF8.GetString(buff);

using (Stream stm = req.GetRequestStream())
{
    bool stmIsReadable = stm.CanRead;  //false, stream is not readable, how to get
                                       //around this?
                                       //solution: read Byte Array into a String,
                                       //modify <wsa:to>, write String back into a
                                       //Buffer, write Buffer to Stream

    buffString = buffString.Replace("http://localhost/WS/Service.asmx", WSaddress);

    //write modded string to buff
    buff = System.Text.UTF8Encoding.UTF8.GetBytes(buffString);

    stm.Write(buff, 0, (int)buff.Length);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 System.Web.HttpRequest 获取协议版本? - How do I get protocol version from System.Web.HttpRequest? 如何在“天蓝色函数”上模拟 HttpRequest - How do I mock the HttpRequest on an "azure function" 如何在Visual Studio 2013中将Web参考从一种解决方案复制到另一种 - How do I copy a web reference from one solution to another in Visual Studio 2013 如何模拟另一个Web服务? - How can I mock another web service? 如何使用SharePoint Copy Web服务的CopyIntoItems方法? - How do you use the CopyIntoItems method of the SharePoint Copy web service? 如何获取httprequest内容以在C#中的localhost上显示? - How do I get httprequest content to display on localhost in c#? 如何将一些自定义数据与当前的HttpRequest相关联? - How do I associate some custom data with current HttpRequest? 如何将Web服务中定义的类型提供给另一个Web服务(共享类型)? - How do you supply a type that's defined in a web service to another web service (share types)? 您如何从另一个Web API服务中调用Web API服务? - How do you call a web api service from another web api service? 如何读取HttpRequest的主体发布到REST Web服务 - C#WCF - How to read body of HttpRequest POSTed to a REST web service - C# WCF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM