-1
我有一個Web服務託管在與SQL Server交互的服務上。 我必須開發一個Windows Phone 8應用程序,該應用程序應該與該Web服務進行交互以從服務器獲取數據。 我爲使用Web客戶端,但得到的迴應:遠程服務器返回錯誤NOTFOUND」 我不知道如何調用一個方法.. ,哪一個更好 了HTTPClient Web客戶端 或任何其他方法在Windows Phone 8中調用Web服務方法應用
我有一個Web服務託管在與SQL Server交互的服務上。 我必須開發一個Windows Phone 8應用程序,該應用程序應該與該Web服務進行交互以從服務器獲取數據。 我爲使用Web客戶端,但得到的迴應:遠程服務器返回錯誤NOTFOUND」 我不知道如何調用一個方法.. ,哪一個更好 了HTTPClient Web客戶端 或任何其他方法在Windows Phone 8中調用Web服務方法應用
我想這應該是幫助您:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/webservicelogin/webservice.asmx/ReadTotalOutstandingInvoice");
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)";
request.CookieContainer = cookie;
// 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);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
//postData value
string postData = "xxxxxxxxxx";
// 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 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 read = streamRead.ReadToEnd();
//respond from httpRequest
TextBox.Text = read;
// Close the stream object
streamResponse.Close();
streamRead.Close();
response.Close();
}
public ConstructoreName()
{
InitializeComponent();
ServiceReferenceCustomer.OfferhutCustomerClient ohCustomer = new ServiceReferenceCustomer.OfferhutCustomerClient();
ohCustomer.getOfferAsync(3); //here getOffer is a method and 3 is a parameter
ohCustomer.getOfferCompleted += new EventHandler<getOfferCompletedEventArgs>(getOffer_completed);
}
void getOffer_completed(object sender, getOfferCompletedEventArgs e)
{
ServiceReferenceCustomer.offer res;
res = e.Result;
offerTitle.Text = res.title;
offerFirstPara.Text = res.shopName + " \n" + res.title + " \n" + res.date;
offerSecendPara.Text = res.description;
}
我認爲這是給你的幫助..