在这里,您如何使用WebRequest
HttpWebResponse response = null;
StreamReader reader = null;
Stream dataStream = null;
StringBuilder sbReadBuffer = null;
int bufSizeMin = 8192;
int bufSizeMax = 65536;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(TheWebPageToRead);
if (req != null)
{
req.Method = "GET";
response = (HttpWebResponse)req.GetResponse();
if (response != null)
{
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream, true);
// get the length of the content returned by the request
int length = (int)response.ContentLength;
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;
// allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
sbReadBuffer = new StringBuilder(bufSize);
// read the whole response
while ((length = dataStream.Read(buf, 0, buf.Length)) != 0){
sbReadBuffer.Append(Encoding.UTF8.GetString(buf, 0, length));
}
}
}
}
finally
{
if (response != null)
response.Close();
if (reader != null)
reader.Close();
if (dataStream != null)
dataStream.Close();
}
这TheWebPageToRead
是你的网址。
保留回报并sbReadBuffer
要求"testers"
我得到:
{"sentences":[{"trans":"Тестеры","orig":"testers","translit":"Testery","src_translit":""}],"src":"en","server_time":11}
要求"aristos"
,我得到:{"sentences":[{"trans":"аристократов","orig":"aristos","translit":"aristokratov","src_translit":""}],"src":"en","server_time":5}