HttpWebRequest“POST"返回服务器错误(异常) [英] HttpWebRequest "POST" returning server error (Exception)

查看:40
本文介绍了HttpWebRequest“POST"返回服务器错误(异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用 google URL Shortner API一个 WP7 应用程序,我首先在 控制台应用程序 上尝试这样做:

I'm trying to call the google URL shortner API from a WP7 app, I tried first on a Console Application by doing this:

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"longUrl\":\"http://www.google.com/\"}";
            Console.WriteLine(json);
            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            Console.WriteLine(responseText);
        }
        Console.Read();

它运行良好,返回一切正常,但是当我尝试在 Windows Phone 应用程序 上这样做时:

and it worked fine, and returned everything OK, but when I try to do it on a Windows Phone App like this:

    private void button1_Click(object sender, RoutedEventArgs e)
    {            
        testConnection();
    }

    private void testConnection()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
            MessageBox.Show("There's no internet connection, please reconnect to the internet and try again");
        else
        {                
            var req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
            req.ContentType = "application/json";
            req.Method = "POST";
            req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req);
            textBlock2.Text = "Done";               

        }
    }
    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the stream request operation
        Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);

        // Create the post data
        // Demo POST data:
        string postData = "http://www.google.com";
        byte[] byteArray = Encoding.Unicode.GetBytes(postData);

        // Add the post data to the web request            
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
    }


    void GetResponseCallback(IAsyncResult asynchronousResult)
    {

        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response;
            // End the get response operation
            response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamResponse);
                            textBlock1.Text= streamReader.ReadToEnd();
            streamResponse.Close();
            streamReader.Close();
            response.Close();

        }
        catch (WebException e)
        {

        }
    }

代码总是转到 try 方法的捕获并给出未找到"错误.

The code always goes to the catch of the try method and gives "not found" error.

推荐答案

SSL 和不受信任的证书存在一个已知问题.当前的 Windows Phone 7 应用程序模型不支持通过 SSL 连接到需要 ClientCertificates 的网站(仅支持 BasicAuthentication).您可以在此处阅读相同的问题:http://forums.create.msdn.com/forums/p/65076/398730.aspx

There is a known issue with SSL and not-trusted certificates. Connection to a web site with SSL that require ClientCertificates is not supported in the current Windows Phone 7 application model (Only BasicAuthentication is supported). You can read about the same problem here: http://forums.create.msdn.com/forums/p/65076/398730.aspx

这篇关于HttpWebRequest“POST"返回服务器错误(异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆