【问题标题】:WindowsPhone HttpWebRequest to ashx WebServiceWindowsPhone HttpWebRequest 到 ashx WebService
【发布时间】:2015-07-24 21:37:39
【问题描述】:

我对 HttpWebRequest 有疑问:

我在 Visual Studio 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 获取代码,所以我不知道我在做什么错,我也尝试了很多在互联网上找到的方法,对我来说唯一重要的是我必须使用 HttpWebRequest 和不是HttpClient

有人可以帮帮我吗?

【问题讨论】:

    标签: c# httpwebrequest ashx


    【解决方案1】:

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

    如果一切正常,请查看此帖子: httpclient postasync windows phone 8.1 universal app second call 404 error

    【讨论】:

    • 老实说,我不知道,这个 ashx 文件是添加到已存在的 WS 中的新文件,该文件由 asmx 制成,被数百部手机使用,我必须对其进行不同的配置才能由 httpwebresponse 调用?
    【解决方案2】:

    好的,真的感谢上帝是星期五。 我使用的是 url "localhost/eccecc.ashx"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多