HttpRequest凭证

时间:2014-10-27 14:52:58

标签: c# soap httprequest

我想使用soap服务,它接收xml数据,并发送过xml数据。 在控制台应用程序中,我使用以下代码进行响应:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("SaopUrl");

            request.Credentials = new NetworkCredential("login", "password");
            request.Method = "POST";
            request.PreAuthenticate = true;
            string postData = @"query_xml=<MY-XML>";

            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream ();
            dataStream.Write (byteArray, 0, byteArray.Length);
            dataStream.Close ();
            WebResponse response = request.GetResponse ();
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            dataStream = response.GetResponseStream ();
            StreamReader reader = new StreamReader (dataStream);
            string responseFromServer = reader.ReadToEnd ();
            Console.WriteLine(responseFromServer);
            reader.Close();
            dataStream.Close();
            response.Close();

但作为回应,我没有获得授权。 如果我使用这样的html表单动作:

<form action="soapUrl" method="post" style="padding:0px">
<table cellpadding="5px" cellspacing="0px">
    <tr>
        <td>
            <textarea name="query_xml" name="query_xml" cols="100" rows="30"></textarea>        
        </td>
    </tr>
    <tr>
        <td align="center">
            <input type="submit" value="Send" />
        </td>
    </tr>
</table>
</form>

我得到了很好的回应。如何使用HttpRequest在C#中执行此操作?

1 个答案:

答案 0 :(得分:0)

Visual studio可以帮助您解决此问题