尝试使用HttpWebRequest进行POST调用时出现安全性异常

时间:2016-09-08 13:32:11

标签: c# silverlight cross-domain httpwebrequest silverlight-5.0

我在尝试调用https端点时收到System.Security.SecurityException [不是Web服务,而是服务器上托管的.Net web api] 我添加了一个clientaccesspolicy文件,http和https都是

        <?xml version="1.0" encoding="UTF-8"?>

    <access-policy>


    <cross-domain-access>


    <policy>


    <allow-from http-request-headers="*">

    <domain uri="https://*"/>

    <domain uri="http://*"/>

    </allow-from>


    <grant-to>

    <resource include-subpaths="true" path="/"/>

    </grant-to>

    </policy>

    </cross-domain-access>

    </access-policy>

Corssdomain xml看起来像这样

  <?xml version="1.0"?>

<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

  <allow-http-request-headers-from headers="*" domain="*"/>

</cross-domain-policy>

我遇到异常的地方是尝试阅读回复

HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

例外情况在这里

 {System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.Brow​serHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at SLLAbapp.Views.ChildWindowConfirmExport.ResponseReady(IAsyncResult asyncResult)}

我用来执行此POST的代码就像

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://....."); //url of web api with https
    request.Method = "POST";
    request.ContentType = "application/json";
    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
void RequestReady(IAsyncResult asyncResult) 
{ 
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
    Stream stream = request.EndGetRequestStream(asyncResult); 
    this.Dispatcher.BeginInvoke(delegate() 
    { 
        // Send the post variables 
        StreamWriter writer = new StreamWriter(stream);                
        writer.Write(this.JsonData); //passing actual json data here
        writer.Flush(); 
        writer.Close(); 
        request.BeginGetResponse(new AsyncCallback(ResponseReady), request);               
    }); 
}
void ResponseReady(IAsyncResult asyncResult) 
{
    try
    {
        HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);//getting exception here
        this.Dispatcher.BeginInvoke(delegate()
        {
            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
            {
                // get the result text 
            }

        });
    }
    catch
    {

    }
}

知道这个错误发生的原因吗?我还将crossdomain.xml放在web api

的根目录下

是因为ssl吗?我可以在httpWebrequest上提到ssl的细节吗?

0 个答案:

没有答案