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

本文关键字:Web 服务 请求 HTTP | 更新日期: 2023-09-27 18:34:29

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 方法正在工作,

示例网络马索德:

[WebMethod]
    public string Test()
    {
        return "hello";
    }

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

示例方法不返回数据。

这是我的例子和工作。

[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;
         } 
    }

控制台应用程序代码。(简单你好世界(

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();
    }
}

控制台应用程序代码。(简单你好世界新(

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();
    }
}