繁体   English   中英

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

[英]How to pass HTTP Request to web service

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

示例Web Mathod:

[WebMethod]
    public string Test()
    {

        return "hello";
    }

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

这是我的示例,正​​在工作。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM