本文介绍了当我从Web服务获得响应时,远程服务器返回错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从网络服务获得响应时,远程服务器返回错误异常。


使用System;


使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用Microsoft.Phone.Tasks;

使用System.Net;

使用System。 IO;
$
名称空间LocationSample 

{

    classServiceConnection

    {

        public string Url ="" ;;

        private string postXml;



        public delegate void OnServerEndResponse(string response,int statusCode);

       公共活动OnServerEndResponse OnEndResponse;



        public ServiceConnection()

        {

            Url =" http://13.1.38.89/mdc/servics/gvice.asmx" ;;
$


        }


        public void ServiceCall(string pxml)

        {

            postXml = pxml;

            WebRequest request = WebRequest.Create(Url);

            request.Method =" POST";

            request.ContentType =" text / xml; charset = UTF-8";

           

            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);

        }


        void GetRequestStreamCallback(IAsyncResult asynchronousResult)

        {

            HttpWebRequest webRequest =(HttpWebRequest)asynchronousResult.AsyncState;

           流postStream = webRequest.EndGetRequestStream(asynchronousResult);

            string postData = postXml;

            byte [] byteArray = Encoding.UTF8.GetBytes(postData);

            postStream.Write(byteArray,0,byteArray.Length);

            postStream.Close();

            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback),webRequest);

        }


        void GetResponseCallback(IAsyncResult asynchronousResult)

        {



$


                HttpWebRequest webRequest =(HttpWebRequest)asynchronousResult.AsyncState;

                HttpWebResponse回复;

                  response =(HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

                Stream streamResponse = response.GetResponseStream();

                StreamReader streamReader = new StreamReader(streamResponse);

                string Response = streamReader.ReadToEnd();

                streamResponse.Close();

                streamReader.Close();

                response.Close();

                OnEndResponse(Response,Convert.ToInt32(response.StatusCode));




        }¥b $ b    }
}


 

public btnclik()


{


string id =" 123";


string pxml = @"< soapenv:Envelope xmlns:soapenv ="" http://schemas.xmlsoap.org/soap/envelope/""的xmlns:网页= QUOT;" HTTP://tempuri.org/"">" +

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< soapenv:页眉/>< soapenv:车身><网址:RDevice>" +

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;"<网址:PrimaryEmail>" + id +"< / web:PrimaryEmail>" +

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< / web:RDevice>< / soapenv:Body>< / soapenv:Envelope>";

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ServiceConnection cs = new ServiceConnection();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cs.ServiceCall(pxml);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cs.OnEndResponse + = new ServiceConnection.OnServerEndResponse(serviceConnection_OnEndResponse);


}


我在第3行的GetResponseCallback(IAsyncResult asynchronousResult)中收到错误ie  response =(HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);


如何解决这个问题请让我知道。没有得到如何摆脱这个问题




amair

解决方案

The remote server returned an error Exception when iam getting the response from the webservice.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Phone.Tasks;
using System.Net;
using System.IO;
namespace LocationSample  
{
    class ServiceConnection
    {
        public string Url = "";
        private string postXml;

        public delegate void OnServerEndResponse(string response, int statusCode);
        public event OnServerEndResponse OnEndResponse;

        public ServiceConnection()
        {
            Url = "http://13.1.38.89/mdc/servics/gvice.asmx";

        }

        public void ServiceCall(string pxml)
        {
            postXml = pxml;
            WebRequest request = WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "text/xml;charset=UTF-8";
           
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }

        void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
            string postData = postXml;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }

        void GetResponseCallback(IAsyncResult asynchronousResult)
        {




                HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;
                 response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string Response = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();
                OnEndResponse(Response, Convert.ToInt32(response.StatusCode));


        }
    }
}

 

public btnclik()

{

string id="123";

string pxml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://tempuri.org/"">" +
                        "<soapenv:Header/><soapenv:Body><web:RDevice>" +
                        
                        
                      
                       
                           "<web:PrimaryEmail>" + id + "</web:PrimaryEmail>" +
                        "</web:RDevice></soapenv:Body></soapenv:Envelope>";
            ServiceConnection cs = new ServiceConnection();
            cs.ServiceCall(pxml);
            cs.OnEndResponse += new ServiceConnection.OnServerEndResponse(serviceConnection_OnEndResponse);

}

iam getting the error in GetResponseCallback(IAsyncResult asynchronousResult) at line 3 ie response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

how to solve this problem please let me know.iam not getting how to came out of this problem


amair

解决方案


这篇关于当我从Web服务获得响应时,远程服务器返回错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:39