繁体   English   中英

WindowsPhone HttpWebRequest到ashx WebService

[英]WindowsPhone HttpWebRequest to ashx WebService

我的HttpWebRequest有问题:

我在视觉工作室ws中有一个ashx代码,它可以做到:

public void ProcessRequest(HttpContext context)
    {
        var a = context.Request.Form["a"];
        var b = context.Request.Form["b"];
        context.Response.Write(a + " " + b);
    }

我尝试使用advancedRestClient调用它,但是它可以正常工作,但是如果我使用Windows Phone设备调用它,则会收到未找到的异常,并且请求未达到任何断点。 这是我的WP代码:

    public void PostIt2()
    {

        string url = "http://localhost/blablacode/ecc.ashx";
        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";

        // start the asynchronous operation
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

        // Keep the main thread from continuing while the asynchronous
        // operation completes. A real world application
        // could do something useful such as updating its user interface. 
        allDone.WaitOne();
    }

    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        string postData = "{'a': 'avar','b':'bvar'}";

        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse
        response.Close();
        allDone.Set();
    }

我从MSDN取得了代码,所以我不知道自己在做错什么,我还尝试了很多在Internet上找到的方法,对我来说唯一重要的是我必须使用HttpWebRequest而不是HttpClient

有人能帮助我吗?

问题可能不在代码中。 通过电话可以访问您的服务吗? 您的proyect是否配置了互联网访问权限?

如果一切正常,请查看此帖子: httpclient postasync Windows Phone 8.1通用应用第二次调用404错误

好吧,真的感谢上帝,星期五。 我正在使用网址“ localhost / eccecc.ashx”

设备的本地主机与计算机的本地主机不同。

暂无
暂无

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

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