Beispiel #1
0
		public JSONDoc HttpPost(string RequestUrl, string RequestContent)
		{
			JSONDoc jDoc = null;
			HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(RequestUrl);

			SetBasicAuthHeader(req, credentials);

			req.ContentType = "application/x-www-form-urlencoded";
			req.Method = "POST";
			try
			{
				byte[] bytedata = Encoding.UTF8.GetBytes(RequestContent);
                req.ContentLength = bytedata.Length;
				req.GetRequestStream().Write(bytedata, 0, bytedata.Length);

				WebResponse res = req.GetResponse();

				if (req.HaveResponse && (res != null) && (res.GetResponseStream() != null))
				{
					if (res.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[res.ContentLength];
						int cnt = 0;
						while (cnt < (int)res.ContentLength)
						{
							int bytesLeft = (int)res.ContentLength - cnt;
							cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
						}
						string str = Encoding.UTF8.GetString(buff);
						jDoc = new JSONParser.JSONDoc(str);
					}
					else
					{
						TextReader str = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);
					}
				}
				res.Close();

				return jDoc;
			}
			catch (WebException ex)
			{
				if ((ex.Response != null) && (ex.Response.GetResponseStream() != null))
				{
					if (ex.Response.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[ex.Response.ContentLength];
						int cnt = 0;
						while (cnt < (int)ex.Response.ContentLength)
						{
							int bytesLeft = (int)ex.Response.ContentLength - cnt;
							cnt += ex.Response.GetResponseStream().Read(buff, cnt, bytesLeft);
						}
						string str = Encoding.UTF8.GetString(buff);
						jDoc = new JSONParser.JSONDoc(str);
						throw new SwarmException(jDoc.ToString(), ex);
					}
					else
					{
						TextReader str = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);
						throw new SwarmException(jDoc.ToString());
					}
				}
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw new SwarmException(ex.Message);
			}
			catch (Exception ex)
			{
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw;
			}
		}
Beispiel #2
0
		public JSONDoc HttpGet(string RequestUrl)
		{
			JSONDoc jDoc = null;

			try
			{
				HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(RequestUrl);
				SetBasicAuthHeader(req, credentials);
				req.ContentType = "application/x-www-form-urlencoded";
				req.Credentials = credentials;
				req.Method = "GET";

				WebResponse res = req.GetResponse();
				if (req.HaveResponse && (res.GetResponseStream() != null))
				{
					// Sometimes the stream reader 'stalls' when we try to tokenize while reading 
					//  the stream and only returns the partial result, so need to read the entire
					//  response stream before passing it to the JSON parser
					//TextReader str = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
					//jDoc = new JSONParser.JSONDoc(str);

					// read the entire response buffer into a string
					string str = string.Empty;
					if (res.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[res.ContentLength];
						int cnt = 0;
						while (cnt < (int)res.ContentLength)
						{
							int bytesLeft = (int)res.ContentLength - cnt;
							cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
						}

						str = Encoding.UTF8.GetString(buff);
					}
					else
					{
						// we didn't get a length, so read the response in chunks and dynamically build the string
						// the easy way, we know how much we need to read
						byte[] buff = new byte[buffSize];

						StringBuilder sb = new StringBuilder(stringBuilderSize);
						int bytesRead = 1; // trick it first time through
						while (bytesRead > 0)
						{
							bytesRead = res.GetResponseStream().Read(buff, 0, buffSize);
							if (bytesRead > 0)
							{
								sb.Append(Encoding.UTF8.GetString(buff, 0, bytesRead));
							}
						}
						str = sb.ToString();
					}
					jDoc = new JSONParser.JSONDoc(str);
				}
				res.Close();

				return jDoc;
			}
			catch (WebException ex)
			{
				if ((ex.Response != null) && (ex.Response.GetResponseStream() != null))
				{
					if (ex.Response.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[ex.Response.ContentLength];
						int cnt = 0;
						while (cnt < (int)ex.Response.ContentLength)
						{
							int bytesLeft = (int)ex.Response.ContentLength - cnt;
							cnt += ex.Response.GetResponseStream().Read(buff, cnt, bytesLeft);
						}

						string str = Encoding.UTF8.GetString(buff);

						jDoc = new JSONParser.JSONDoc(str);

						throw new SwarmException(jDoc.ToString(), ex);
					}
					else
					{
						TextReader str = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);

						throw new SwarmException(jDoc.ToString(), ex);
					}
				}
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw new SwarmException(ex.Message, ex);
			}
			catch (Exception ex)
			{
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw;
			}
		}