GET
요청에 대한 내 C# 프로젝트에서 HTTPRequest
을 작동 시키려고 노력 중이며 제대로 작동하지 않습니다.WP8의 HttpWebRequest
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();
}
}
내가 현재 무엇입니까 오류는 다음과 같습니다 : 다음은 내 코드입니다
An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.ni.dll Operation is not valid due to the current state of the object.
"작동하지 않음"에 대해 자세히 설명해 주실 수 있습니까? 또한 URL이 엉망이되어서''http://mycompany.com ''과''createtrip.php "' – Matthew
사이에 슬래시가 없습니다. 죄송합니다. 지금은 임시 URL입니다. 나는 그 변화와 나의 오류 메시지로 메인 포스트를 편집했다. –
오류가 발생한 위치에 대한 스택 추적이 있습니까? – Matthew