2017-02-20 125 views
1

我翻譯一個JSON API爲C#的方法,我遇到了一個問題,即在JSON RPC API(POST)說C#HttpWebRequest的「請求頭」,在JSON POST

其他所有方法都需要從認證結果(=的sessionId),或者每pathparameter

;jsessionid=644AFBF2C1B592B68C6B04938BD26965

或每個Cookie(RequestHeader)

JSESSIONID=644AFBF2C1B592B68C6B04938BD26965

我目前的WebRequest方法:

private async static Task<string> SendJsonAndWait(string json, string url, string sessionId) { 
     string result; 

     HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
     httpWebRequest.ContentType = "application/json"; 
     httpWebRequest.Method = "POST"; 

     using(StreamWriter streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync())) { 
      await streamWriter.WriteAsync(json); 
      streamWriter.Flush(); 
      streamWriter.Close(); 
     } 

     HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync(); 
     Stream responseStream = httpResponse.GetResponseStream(); 
     if(responseStream == null) 
      throw new Exception("Response Stream was null!"); 

     using(StreamReader streamReader = new StreamReader(responseStream)) { 
      result = await streamReader.ReadToEndAsync(); 
     } 

     return result; 
    } 

我如何JSESSIONID參數添加到我的WebRequest?我對WebRequests不是很熟悉,請簡單解釋一下!

謝謝!

+1

您是否嘗試直接添加到您的網址? –

+0

正如@RomEh建議的那樣,您可以將參數添加到Url中作爲查詢字符串參數,或者您可以使用鍵「sessionid」和「json」以及它們的值創建一個字典對象,將其序列化爲json字符串,然後將其添加到請求流。 –

回答

1

使用Cookies

你的情況看起來像這樣;

private async static Task<string> SendJsonAndWait(string json, string url, string sessionId) { 
    Uri uri = new Uri(url); 
    string result; 

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
    httpWebRequest.ContentType = "application/json"; 
    httpWebRequest.Method = "POST"; 

    //Add the JSESSIONID Cookie 
    if(httpWebRequest.CookieContainer == null) 
      httpWebRequest.CookieContainer = new CookieContainer(); 

    if(!string.IsNullOrWhiteSpace(sessionId)) 
      httpWebRequest.CookieContainer.Add(new Cookie("JSESSIONID", sessionId, "/", uri.Host)); 

    using(StreamWriter streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync())) { 
     await streamWriter.WriteAsync(json); 
     streamWriter.Flush(); 
     streamWriter.Close(); 
    } 

    HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync(); 
    Stream responseStream = httpResponse.GetResponseStream(); 
    if(responseStream == null) 
     throw new Exception("Response Stream was null!"); 

    using(StreamReader streamReader = new StreamReader(responseStream)) { 
     result = await streamReader.ReadToEndAsync(); 
    } 

    return result; 
} 
0

您可以直接添加該令牌您的網址:

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create($"{url}?jsessionid={sessionId}"); 

還是在標題:

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create({url); 
httpWebRequest.Headers["JSESSIONID"] = sessionId; 
+0

我試過了,我仍然得到服務器的否定響應。我不確定是否有其他問題導致問題,但.. –

+0

如果它有效,您應該嘗試{url}?; jsessionid = {sessionId}和{url}; jsessionid = {sessionId}。 –