2011-04-27 11 views
0

Couchdb 데이터베이스를 만드는 HTTP PUT 메서드를 가져 오는 데 문제가 있습니다. 코드에 대해 무서운 것은 없지만 전에 보았던 코드는 너무 지루하다. Connection 메서드가 Closed 인 경우 오류 메시지가 나타납니다. 뭔가를 알고 있어야합니다 .... 정말 이상한 것. Couchdb를 사용하여 새로운 DB를 만드는 컨텍스트에서 올바른 POST 메서드를 사용할 때 404가 표시됩니다. 어떤 도움을 많이 주셨습니다. HTTPWebRequest가 PUT 메소드를 수행 할 수 있습니까? 그렇다면 왜 안되는 지에 대해 당황 스럽습니다.Couchdb .... HttpWebRequest

자세한 내용은 다음과 같습니다. 기본 연결이 닫혔습니다 : 예기치 않게 연결이 닫혔습니다.

저는 Couchdb와 CURL을 확인했습니다 ... 괜찮습니다.

오류가이 시점에서 온다 ... 방법을 설정 한 후

Stream requestStream = httpWebRequest.GetRequestStream(); 

코드는 "PUT"로 :

private string DataViaHTTP(string url, Dictionary<string, string> parameters, string content, string contentType, int timeout, bool contentIsParam, string method) 
{ 
    byte[] requestData; 
    try 
    { 
     HttpWebRequest httpWebRequest; 

     if (contentIsParam == false) 
     { 
      requestData = System.Text.Encoding.ASCII.GetBytes(content); 
      httpWebRequest = (HttpWebRequest)WebRequest.Create(BuildParamString(url, parameters)); 
     } 
     else 
     { 
      requestData = System.Text.Encoding.ASCII.GetBytes(BuildParamString(null, parameters)); 
      httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
     } 

     httpWebRequest.Method = method; 
     httpWebRequest.ContentType = contentType; 

     if (timeout > 0) 
     { 
      httpWebRequest.Timeout = timeout; 
     } 

     httpWebRequest.ContentLength = requestData.Length; 
     Stream requestStream = httpWebRequest.GetRequestStream(); 
     requestStream.Write(requestData, 0, requestData.Length); 
     requestStream.Close(); 

     // Read and return the response stream 
     HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

     Stream outStream = httpWebResponse.GetResponseStream(); 


     var stringStream = String.Empty; 
     using (StreamReader streamReader = new StreamReader(outStream)) 
     { 
      stringStream = streamReader.ReadToEnd(); 

     } 

     return stringStream; 
    } 
    catch (WebException e) 
    { 
     throw e; 
    } 
    catch (Exception e) 
    { 

     throw e; 
    } 
} 

이 작동하지 않았다 중 하나 :(

public string PutCommand(string url) 
{ 
    try 
    { 
     using (WebClient webclient = new WebClient()) 
     { 
      webclient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.89 Safari/532.5"; 
      webclient.Encoding = System.Text.Encoding.ASCII; 
      var x = webclient.UploadData(url, "PUT", new byte[] {}); 
      return System.Text.Encoding.ASCII.GetString(x); 
     } 
    } 
    catch (Exception e) 
    { 
     throw e; 
    } 
} 
+1

코드가 지루하더라도 오류가 있어야합니다. – Femaref

+0

내가 원한다면 ...... : – WeNeedAnswers

+0

GET과 POST 둘 다 몇 년 동안 사용해온 동일한 코드 – WeNeedAnswers

답변

1

이것이 문제의 해결 방법이 아니라는 것을 알고 있습니다 만, System.Net.WebClient 클래스를 사용할 수없는 이유가 있습니다. URL에서 데이터를 가져오고 싶습니까? 그것은 당신이 쓴 복잡한 코드를 많이 없앨 것입니다. 다음과 같이 한 줄로 문자 그대로 사용할 수 있습니다.

string data = new WebClient().DownloadString(@"http://whateverURL.com/?options=1&somethingElse=5"); 
+0

단순한 데이터로 본격적으로 사용하고 POST 및 PUT 명령을 수행 할 수 있습니까? 아니면 그냥 얻으십니까? – WeNeedAnswers

+0

링크 된 MSDN 페이지 읽기 ... UploadString() 메소드 HTTP POST를 수행 할 수 있습니다. – qJake

+0

그냥 고마워요 @ SpikeX. 체크 아웃 할 것입니다. 현재의 딜레마를 해결하면 이전 코드를 버리고 webClient에 플러그를 꽂습니다. – WeNeedAnswers