帮我解决以下通过URL取得页面内容时出现的问题
我通过URL连接web server,取得页面的返回信息,web server使用的是websphere,我在读取数据的时候总是出现IOExcepiton错误,具体的错误信息如下:
Exception:System.IO.IOException:块区长度无效。 --->System.FormatException:FormatException
服务器已经正确的接收到我提交的所有数据,并且可以看到返回的状态是200,也没有问题。但是只要一读取数据就出现错误。怀疑是因为服务器在返回数据的时候先发送了一个空格,然后做了一个flush操作,最后才返回真正有用的数据。以下是我的读取数据的程序,请高手指点一下,怎么解决这个问题。
public Hashtable httpPost(String urlString, String urlParams)
{
HttpWebResponse myWebResponse = null;
String content = "";
StringBuilder sb = new StringBuilder();
try
{
//连接请求URL
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlString);
//发送头信息
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = urlParams.Length;
//POST相关参数
StreamWriter myWriter = new StreamWriter(req.GetRequestStream());
myWriter.Write(urlParams);
myWriter.Close();
Console.WriteLine("POST参数完毕!");
//取得返回信息,服务器先发送了一个空格,然后做了一个flush操作,再进行正确数据的输出
myWebResponse = (HttpWebResponse)req.GetResponse();
Console.WriteLine("\nContent length :{0}, Content Type : {1}, Content-Encoding: {2}, myWebResponse.StatusCode:{3}",
myWebResponse.ContentLength,
myWebResponse.ContentType,
myWebResponse.ContentEncoding,
myWebResponse.StatusCode);
Stream ReceiveStream = myWebResponse.GetResponseStream();
int ch;
while((ch = ReceiveStream1.ReadByte()) != -1)
{
//Console.Write("ch = " + ch);
process((byte)ch, sb);
}
myWebResponse.Close();
ReceiveStream.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception :" + e.ToString());
}
finally
{
if ( myWebResponse != null )
{
myWebResponse.Close();
}
}
return sb.ToString();
}
private static void process(byte b, StringBuilder sb)
{
sb.Append((char)b);
}
浙公网安备 33010602011771号