如何将HTTP请求传递给Web服务

时间:2014-12-17 06:27:44

标签: c# asp.net web-services

IDE:VS 2010

我有一个要求,我必须从HTTP请求中使用服务(不在客户端项目中添加服务引用。)我已经学习了很多教程,但总的来说我得到错误505,同时制作

System.Net.HttpWebResponse resp =(System.Net.HttpWebResponse) req.GetResponse();

以下是我使用过的示例代码之一

        System.Net.WebRequest req = System.Net.WebRequest.Create("http://localhost:8090/MyService.asmx");
        //req.Proxy = new System.Net.WebProxy("127.0.0.1", true);
        //req.Htt("POST /_22YardzWebService.asmx/ReceiveXMLByContent HTTP/1.1");
        //builder.Append("Host: localhost");
        req.ContentType = "text/xml; encoding='utf-8'";
        //req.ContentLength = 4096;

        //Add these, as we're doing a POST
        //req.ContentType = "application/x-www-form-urlencoded";

        req.Method = "POST";
        //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
        byte[] bytes = Encoding.UTF8.GetBytes(Parameters);
        req.ContentLength = bytes.Length;


        //System.IO.Stream os = req.GetRequestStream();
         //Push it out there

        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        //os.Close();

        System.Net.HttpWebResponse resp =(System.Net.HttpWebResponse) req.GetResponse();
        if (resp == null) return null;
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        return sr.ReadToEnd().Trim();  

代码来源:http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

从服务引用调用时,Web方法正常工作

示例网络Mathod:

[WebMethod]
    public string Test()
    {

        return "hello";
    }

1 个答案:

答案 0 :(得分:2)

您的示例方法不会返回数据。

这是我的例子和工作。

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
         public string HelloWorldNew(string data1,string data2)
         {
          return "Hello World" + data1 + data2;
         } 
    }

控制台应用程序代码。 (简单的HelloWorld)

class Program
{
    static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:60395/Service1.asmx/HelloWorld");
        request.Method = "POST";
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string data = reader.ReadToEnd();
    }
}

控制台应用程序代码。 (简单的HelloWorldNew)

class Program
{
    static void Main(string[] args)
    {
        string data = "data1=user1&data2=user2";
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:60395/Service1.asmx/HelloWorldNew");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataStream.Length;
        Stream newStream = request.GetRequestStream();            
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string dataReturn = reader.ReadToEnd();
    }
}