无法在C#中获得WebResponse [英] unable to get webresponse in c#

查看:122
本文介绍了无法在C#中获得WebResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送肥皂请求,但在webresponse上出现错误.
远程服务器返回错误:(404)找不到.
我已经在SOAPUI中测试了xml,并且在那里给出了有效的响应.
可能是什么原因. 请帮忙

I am trying to send a soap request but getting an error on webresponse.
The remote server returned an error: (404) Not Found.
I have tested the xml in SOAPUI and it is giving a valid response there.
what could be the reason .
Please help

string soap =
                        @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Header>
<AuthHeader xmlns=""http://stellatravelgateway.stellatravelservices.co.uk"">119d8a82-0d5b-4893-****-************</AuthHeader>
</s:Header><s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Search xmlns=""http://stellatravelgateway.stellatravelservices.co.uk/AirService"">
<request SearchType=""Availability"" FlexiDays=""0"" FareType=""All"" SearchSource=""Default"" ReturnIncompleteResults=""false"" SortOrder=""Price"" ChildPaxCount=""1"" AdultPaxCount=""1"" DirectFlightsOnly=""false"" CabinClass=""All"" InfantWithSeatPaxCount=""0"" InfantPaxCount=""1"">
                         <SelectedAirlines><string>BA</string></SelectedAirlines>
            <MaximumConnectionTimeMins xsi:nil=""true""/><MaxResultsPerAirline xsi:nil=""true""/>
                <JourneyDetails>
<JourneyDetail DepartureDateTime=""2014-09-01T00:00:00"" DeparturePoint=""lon"" DeparturePointIsCity=""false"" DestinationPoint=""bcn"" DestinationPointIsCity=""false"" ViaPointAirportCode=""/>
                    <JourneyDetail DepartureDateTime=""2014-09-08T00:00:00"" DeparturePoint=""bcn"" DeparturePointIsCity=""false"" DestinationPoint=""lon"" DestinationPointIsCity=""false"" ViaPointAirportCode=""/>
                        </JourneyDetails>
            <MaxResults xsi:nil=""true""/>
                <IncludePriorDepartues>false</IncludePriorDepartues>
            <SessionID>c80b7581-28b8-4b04-829f-d9a3e79b5614</SessionID>
                </request>
                </Search>
                </s:Body>
                    </s:Envelope>

";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stellatravelgateway.stellatravelservices.co.uk/AirService");
        req.Headers.Add("SOAPAction", "http://stellatravelgateway.stellatravelservices.co.uk/AirService/IAirService/Search");
        req.ContentType = "text/xml;charset=utf-8";
        req.Accept = "text/xml";
        req.Method = "POST";

        using (Stream stm = req.GetRequestStream())
        {
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(soap);
            }
        }

        WebResponse response = req.GetResponse();

        Stream responseStream = response.GetResponseStream();
        // TODO: Do whatever you need with the response

.

推荐答案

[新解决方案]
添加具有给定地址的Web服务引用.然后使用以下代码代替xml.

[new solution]
add web service reference with the given address. then use the following code instead of xml.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Test_WebSvc
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ServiceReference1.AirServiceClient cl = 
                    new ServiceReference1.AirServiceClient();
                /*
                you need credentials
                cl.ClientCredentials.UserName
                */
                cl.Open();

                cl.Search(new ServiceReference1.AirSearch()
                {
                    SelectedAirlines = new String[] { "BA" }
                });
                cl.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}


这篇关于无法在C#中获得WebResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆