Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP8上的HttpWebRequest_C#_.net_Windows Phone 8 - Fatal编程技术网

C# WP8上的HttpWebRequest

C# WP8上的HttpWebRequest,c#,.net,windows-phone-8,C#,.net,Windows Phone 8,我一直在尝试让HTTPRequest在我的C#项目中为一个get请求工作,但我不能完全让它工作。下面是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Windows; using System.Diagnos

我一直在尝试让
HTTPRequest
在我的C#项目中为一个
get
请求工作,但我不能完全让它工作。下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows;
using System.Diagnostics;
using System.Threading;

class MyClass 
{
    const string URL_PREFIX = "http://mycompany.com/";
    private HttpWebRequest objRequest = null;
    private static string myRequestData = string.Empty;
    private string urlAddress;


    public MyClass()
    {
        int member = 1;
        int startLoc = 1;
        int endLoc = 1;
        string starttime = "2012-01-01 00:00:00";
        string endtime = "2012-01-01 00:00:00";
        int rt = 1;
        string cmt = "Hello World";

        this.urlAddress = URL_PREFIX + string.Format(
        "createtrip.php?member={0}&startLoc={1}&endLoc={2}&starttime={3}&endtime={4}&rt={5}&cmt={6}"
        , member, startLoc, endLoc, starttime, endtime, rt, cmt);

        StringBuilder completeUrl = new StringBuilder(urlAddress);
        objRequest = (HttpWebRequest)WebRequest.Create(urlAddress);
        objRequest.ContentType = "application/x-www-form-urlencoded";

        objRequest.BeginGetRequestStream(new AsyncCallback(httpComplete), objRequest);
    }
    private static void httpComplete(IAsyncResult asyncResult)
    {
        HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
        // End the operation
        Stream postStream = objHttpWebRequest.EndGetRequestStream(asyncResult);
        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(myRequestData);
        // Write to the request stream.
        postStream.Write(byteArray, 0, myRequestData.Length);
        postStream.Close();

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

    }
    private static void GetResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
        HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.EndGetResponse(asyncResult);
        Stream objStreamResponse = objHttpWebResponse .GetResponseStream();
        StreamReader objStreamReader = new StreamReader(objStreamResponse );
        string responseString = objStreamReader.ReadToEnd();            // Got response here
         MessageBox.Show("RESPONSE :" + responseString);
        // Close the stream object
        objStreamResponse .Close();
        objStreamReader.Close();
        objHttpWebResponse.Close();
    }

}
我当前遇到的错误是:


在mscorlib.ni.dll中发生类型为“System.Collections.Generic.KeyNotFoundException”的异常,在托管/本机边界之前未处理该异常
System.Windows.ni.dll中发生了类型为“System.Net.ProtocolViolationException”的第一次意外异常
由于对象的当前状态,操作无效。

您不能将方法“BeginGetRequestStream”用于GET或HEAD请求(GET是默认的方法,也是您在第一个HTTP请求中执行的方法)

更改为使用“BeginGetResponse”,正如您在代码第二部分中所做的那样。

您不能将方法“BeginGetRequestStream”用于GET或HEAD请求(GET是默认的方法,也是您在第一个HTTP请求中执行的方法)


更改为使用“BeginGetResponse”,正如您在代码的第二部分中所做的那样。

我建议您使用功能强大的库“Microsoft HTTP客户端库”,该库需要.NET 4并可与WP7、WP8、Silverlight 4-5、Windows应用商店应用程序、可移植类库一起使用

您可以简单地从NuGet添加它,并非常简单地使用它

下面是HTTP GET的一个示例

        private async Task PerformGet()
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(myUrlGet);
            if (response.IsSuccessStatusCode)
            {
                // if the response content is a byte array
                byte[] contentBytes = await response.Content.ReadAsByteArrayAsync();

                // if the response content is a stream
                Stream contentStream = await response.Content.ReadAsStreamAsync();

                // if the response content is a string (JSON or XML)
                string json = await response.Content.ReadAsStringAsync();

                // your stuff..
            }
        }

我建议您使用功能强大的库“Microsoft HTTP客户端库”,该库需要.NET 4并可与WP7、WP8、Silverlight 4-5、Windows应用商店应用程序和可移植类库一起使用

您可以简单地从NuGet添加它,并非常简单地使用它

下面是HTTP GET的一个示例

        private async Task PerformGet()
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(myUrlGet);
            if (response.IsSuccessStatusCode)
            {
                // if the response content is a byte array
                byte[] contentBytes = await response.Content.ReadAsByteArrayAsync();

                // if the response content is a stream
                Stream contentStream = await response.Content.ReadAsStreamAsync();

                // if the response content is a string (JSON or XML)
                string json = await response.Content.ReadAsStringAsync();

                // your stuff..
            }
        }

你能详细说明一下“不工作”吗?此外,您的URL是乱七八糟的,
之间没有斜杠。”http://mycompany.com“
createtrip.php”
对不起,这是SO的临时url。我已编辑了带有该更改和错误消息的主要帖子。您是否有错误发生位置的堆栈跟踪?mscorlib.ni.dll中发生了类型为“System.Collections.Generic.KeyNotFoundException”的异常,在托管/本机边界之前未得到处理类型为“System.Net.ProtocolViolationException”的第一次意外异常在System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(异步回调,对象状态)的System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(异步回调,对象状态)的LongGreyline.Trip..ctor()的System.Windows.ni.dll中发生。您确定这实际上是一个致命异常吗?它可能在ClientHttpWebRequest本身中被捕获……您能详细说明一下“不工作”吗?此外,您的URL是乱七八糟的,
之间没有斜杠。”http://mycompany.com“
createtrip.php”
对不起,这是SO的临时url。我已编辑了带有该更改和错误消息的主要帖子。您是否有错误发生位置的堆栈跟踪?mscorlib.ni.dll中发生了类型为“System.Collections.Generic.KeyNotFoundException”的异常,在托管/本机边界之前未得到处理类型为“System.Net.ProtocolViolationException”的第一次意外异常在System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(异步回调,对象状态)的System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(异步回调,对象状态)的LongGreyline.Trip..ctor()的System.Windows.ni.dll中发生。您确定这实际上是一个致命异常吗?它可能在ClientHttpWebRequest本身中被捕获…嗨,我想知道你是否已经解决了关于“绘制kdop”的问题,也许你思考这个问题需要很长时间。如果你能帮我解决同样的问题,我将不胜感激。希望你的回复。我的电子邮件是:tl3shi@gmail.com.Idid调用了BeginGetResponse spAuthReq.BeginGetResponse(新的异步回调(GetResponsetStreamCallback),spAuthReq);但是我得到的响应是System.Windows.ni.dll中第一次出现类型为“System.Net.ProtocolViolationException”的异常。错误:使用此方法的请求不能有请求正文。您好,我想知道您是否解决了“绘制kdop”的问题,也许您思考这个问题需要很长时间。如果你能帮我解决同样的问题,我将不胜感激。希望你的回复。我的电子邮件是:tl3shi@gmail.com.Idid调用了BeginGetResponse spAuthReq.BeginGetResponse(新的异步回调(GetResponsetStreamCallback),spAuthReq);但是我在System.Windows.ni.dll中第一次遇到“System.Net.ProtocolViolationException”类型的异常时得到了响应。错误:使用此方法的请求不能有请求正文。